0

I am trying connect to host machibe with SSH.NET library. Here is my code;

`using(var client new SSHClient(host,port,username,password))
{
  try 
  {
    client.HostReceived += delegate (object sender, HostKeyEventArgs e {e.CanTrust = true;});
    client.Connect()
    if(client.IsConnected)
    {
      var command ="some command";
      var sshcommand = client.RunCommand(command) or SShCommand sshcommand = client.CreateCommand(command); sshcommand.Execute() or client.BeginExecute();
      Console.WriteLine(sshcommand.Result)
   }
   catch (SShConnectionException e)
   {
      Console.WriteLine(e.DisconnectReason);
      Console.WriteLine(e);
   }

}`

I tried SshClient with ConnectionInfo constructor also. Connection is establishing with host machine. However, code is givin SSHConnectonException with messages that ConnectionLost or Connection aborted in the client.RunCommand(command) or SShCommand sshcommand = client.CreateCommand(command); client.Execute() lines. When i try ssscommand.BeginExecute result is null and sshCommand.IsCompleted is false.

What i am missing? Why after succesfull connection, connection is lost in sshcommand?

  • You may be going through a proxy. How long does it take before exception occurs. If it is 30 seconds it mean the connection never completed and you got a timeout. Are you trying to connect to a linux machine? Did you check the log files on the Linux? Can you connect using Putty? – jdweng Aug 21 '23 at 12:10
  • Yes it is a Linux Machine. I can connect from my windows machine with cmd using ssh commands. I do not think it is about proxy. I can connect Putty and Smartttyy as well. – Emre Polat Aug 21 '23 at 13:13
  • The issue is authentication (see https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement). It is working with Putty but not c#. The ssh.net library is using wrong authentication method. See following : https://stackoverflow.com/questions/30667142/ssh-net-authenticate-via-private-key-only-public-key-authentication – jdweng Aug 21 '23 at 13:21
  • I can succesfully send a command with ShellStream right now (client.CreateShellStream("",80,24,800,600,1024)). I used stream writer and reader also. I do not think it is a authentication problem. If it is a authentication problem how can i connect and send a command with shell stream without private key? – Emre Polat Aug 23 '23 at 08:37
  • You are comparing apples with oranges. There is more than one type authentication and different protocols. Unless you are comparing the same protocol and the same authentication method you cannot say the issue is not authentication. – jdweng Aug 23 '23 at 12:10
  • Yes. I don't have detailed information about SSH protocol and authentication. I need part of the project. However, when i debug the code, sshclient's connection info parameters (AuthenticationMethods, ClientEncryption, ServerEncryption, HostKeyAlgorithm ....etc) are the same between working version and not working version. ThaT's why i told, i do not think it's a authentication problem. I tried to Key-Based authentication also but i don't have passphrase parameters. I think that's why i failed. Right now, it's working with Shell Stream. Is shell stream is good or should i try with sshcommand? – Emre Polat Aug 24 '23 at 08:03
  • Did you check the server log files? – jdweng Aug 24 '23 at 09:08

0 Answers0