0

I am using the SSH.NET library in C# to connect and communicate with an SSH server. I have no errors initially connecting to the server with the below lines:

sshClient = new SshClient(IpAddress, mySshUsername, myEthernetPassword);
sshClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);  // Increased timeout for longer wait times
sshClient.Connect();

The problem is when I try to issue commands, it hangs indefinitely at the call to command.Execute():

var command = sshClient.CreateCommand("my_command");
var result = command.Execute();

So I tried to investigate this by connecting to the server via Putty. When I connect to the server via Putty, after the initial connection, the server sends the string "Connected to server". After that, I can send commands as needed and I get the expected responses. Since putty was working, I looked into the ShellStream class in SSH.NET. So instead of commands, I did the following after connecting to the server:

shellStream = sshClient.CreateShellStream("vt100", 80, 24, 800, 600, 4096);
string response = shellStream.ReadLine();

When I use ShellStream instead of command, it doesn't hang, and the initial ReadLine() returns the string "Connected to server", which is expected and what I saw in putty. However, the problem is that any subsequent messages sent to the server like below, result in an empty string, or to be more exact, a return of "/r/0".

shellStream.WriteLine(sendStr);
shellStream.Flush();  // This will flush the buffer to the remote system
string response = string.Empty;
    
while (string.IsNullOrEmpty(response))
{
    response = shellStream.Read();
    await Task.Delay(50);
}

So using ShellStream is a little better in that at least it doesn't hang and I get that expected initial "connected" string. But I don't understand why subsequent messages I send don't come back with a proper response.

For the record, these messages I am sending are from a defined API, where the server should be reporting back device information. The device has HTTP and Telnet servers as well. When I connect via one of those protocols and send a command from the API such as "VERSION?", they will return something like "1.2.3". So I know the commands are valid. But via SSH with shellstream I am getting nothing back, and via Command.Execute in SSH it just hangs.

Any insight or further debugging tips would be appreciated.

0

1 Answer 1

0

The solution to this problem was to confirm the expected EOL character for my SSH server. In my case it was LF ('\n'). Once I appended LF to the end of my command strings and used ShellStream with the simple Write function (as opposed to WriteLine), it worked and I now receive the responses from the server that I expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.