简体   繁体   中英

how display the output from the shellStream

I have developer a web application in asp.net .I use ssh.net to establish a connection between my application and the Cisco devices. i use the below code:

  1. to connect

      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(); 
  2. to run command i use 2 way:

    2.1.

      var cmd = sshClient.RunCommand("show user"); Label1.Text = cmd.Result; 

    its work fine withe router and switch but not working with firewall for that i try to use 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)); 

i can send multiple command to divice but my probbleme is how to display the result of this command from the shellStream. i try some thing like that but is not working

string reslt = Console.ReadLine();
Label1.Text = Reslt;

i try to mixed this 2 way like this

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;

but is not working and i have this Exception

Additional information: Failed to open a channel after 10 attempts.

my question is how display the output from the shellStream ?

The only way I found that works is to connect then make a read and write stream and use it to pass commands and read output, so:

Renci.SshNet.SshClient client = new Renci.SshNet.SshClient(server_name, username, password); client.Connect(); Renci.SshNet.ShellStream stream = client.CreateShellStream("dumb",0 ,0 ,0 ,0 ,1000);

Now that you have a stream opened you can write commands like:

stream.Write(string command + "\\n");

or read it like this:

string temp_string = stream.Read();

Only problem is you need to time out for the duration of command before you read an output, I used empty "while" until expected string with my username was read and then broke out of it.

The question is old. But I hope this can still help someone.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM