最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Cannot read responses from SSH server using SSH.NET library - Stack Overflow

programmeradmin4浏览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.

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.

Share Improve this question asked Mar 24 at 15:59 JimJamFlimFlamJimJamFlimFlam 1 1
  • 1 If the remote host is a specialized device instead of a general-purpose computer, the SSH server may not support command.execute(). Your while (string.IsNullOrEmpty(response)) looks like it stops reading once it reads anything. See if the server sends additional text. – Kenster Commented Mar 24 at 21:25
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论