如何显示 shellStream 的输出

mar*_*wen 0 c# asp.net

我在 asp.net 中开发了一个 Web 应用程序。我使用 ssh.net 在我的应用程序和 Cisco 设备之间建立连接。我使用下面的代码:

  1. 连接

        var ip = DropDownList2.SelectedItem.Text;
        var user = txtuser.Text;
        var passw = txtpass.Text;
        var connInfo = new Renci.SshNet.PasswordConnectionInfo(ip, 22, user, passw);
        var sshClient = new Renci.SshNet.SshClient(connInfo);
        try
        {
            sshClient.Connect();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 要运行命令,我使用两种方式:

    2.1.

            var cmd = sshClient.RunCommand("show user");
    
            Label1.Text = cmd.Result;
    
    Run Code Online (Sandbox Code Playgroud)

    它可以在路由器和交换机上正常工作,但不能与防火墙一起使用,因为我尝试使用shellStream

    2.2.

             var ss = this.shellStream;
             sshClient.Connect();
             this.shellStream = sshClient.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
    
             Console.WriteLine(SendCommand("enable", ss));
    
             Console.WriteLine(SendCommand(passw, ss));
    
             Console.WriteLine(SendCommand("show looging", ss));
    
    Run Code Online (Sandbox Code Playgroud)

我可以向设备发送多个命令,但我的问题是如何从 shellStream 显示该命令的结果。我尝试了类似的事情但不起作用

string reslt = Console.ReadLine();
Label1.Text = Reslt;
Run Code Online (Sandbox Code Playgroud)

我尝试像这样混合这两种方式

this.shellStream = sshClient.CreateShellStream("dumb", 80, 24, 800, 600, 1024);

Console.WriteLine(SendCommand("enable", ss));

Console.WriteLine(SendCommand(passw, ss));

Renci.SshNet.SshCommand cmd;

cmd = sshClient.RunCommand("show logging");

txtenablepass.Text = cmd.Result;
Run Code Online (Sandbox Code Playgroud)

但不起作用,我有这个异常

附加信息:尝试 10 次后未能打开频道。

我的问题是如何显示 shellStream 的输出?

Sum*_*udu 6

这个问题很老了。但我希望这仍然可以帮助别人。

var connInfo = new Renci.SshNet.PasswordConnectionInfo("<IP>", 22, "<USER>", "<PWD>");
var sshClient = new Renci.SshNet.SshClient(connInfo);

sshClient.Connect();
var stream = sshClient.CreateShellStream("", 0, 0, 0, 0, 0);

// Send the command
stream.WriteLine("echo 'sample command output'");

// Read with a suitable timeout to avoid hanging
string line;
while((line = stream.ReadLine(TimeSpan.FromSeconds(2))) != null)
{
    Console.WriteLine(line);
    // if a termination pattern is known, check it here and break to exit immediately
}
// ...
stream.Close();
// ...
sshClient.Disconnect();
Run Code Online (Sandbox Code Playgroud)