Multiple commands in sequence (C#)

Sep 22, 2011 at 1:18 PM
Edited Sep 22, 2011 at 1:30 PM

how do i run multiple commands and catch the output of each command - i know you can chain the commands ";" but have no clue on how to get the output of each command into separate variables.

the way ive done is so far has been to disconnect and connect again after each command, but this isnt sufficient anymore.

[EDIT]

after reading through the documentation - i want to run these on seperate channels!.

so if anyone have any examples that would be great

thanks in advance

Coordinator
Sep 23, 2011 at 4:52 PM

You can execute it this way.

Sorry about pure documention, dont have time to update it :(

using (var client = new SshClient("host", "username", "password"))
{
    client.Connect();
    var cmd = client.RunCommand("echo 1");
    cmd.Execute();
    cmd = client.RunCommand("echo 2");
    cmd.Execute();
    cmd = client.RunCommand("echo 3");
    cmd.Execute();
    client.Disconnect();
}


That should work, I didnt run it on my machine yet, since I rebuilding it now, but the idea is once you open the connection you can reuse it to run multiple commands.

If you want to run them on multiple channels in async mode you can do this:

using (var client = new SshClient("host", "username", "password"))
{
client.Connect();
var cmd = client.CreateCommand("sleep 30s;date"); // Perform long running task
var asynch = cmd.BeginExecute(null, null);

var cmd1 = client.CreateCommand("sleep 30s;date"); // Perform long running task
var asynch1 = cmd1.BeginExecute(null, null);

while (!(asynch.IsCompleted && asynch1.IsCompleted))
{
    Console.WriteLine("Waiting for commands to complete...");
    Thread.Sleep(2000);
}
cmd1.EndExecute(asynch1);
cmd.EndExecute(asynch);
client.Disconnect();
}

May be not the best example since I prefer not to use Thread.Sleep but Wait method but still, it does the trick.

 

Hope it helps.

Thanks,

Oleg

Sep 23, 2011 at 6:09 PM
using (var client = new SshClient("host", "username", "password"))
{
    client.Connect();
    var cmd = client.RunCommand("echo 1");
    cmd.Execute();
    cmd = client.RunCommand("echo 2");
    cmd.Execute();
    cmd = client.RunCommand("echo 3");
    cmd.Execute();
    client.Disconnect();
}

in the above context.. would i be able to get the output of each seperate "cmd" or would i only be able to get output from well the last one i guess?


Coordinator
Sep 23, 2011 at 6:12 PM

well, after execution of each command you will get the output of last executed command,

if you want to keep them in memory and analyze later then you can create new command each time

var cmd1 =

var cmd2 =

var cmd3 =

 

etc'.

Sep 23, 2011 at 6:22 PM
Edited Sep 23, 2011 at 6:29 PM
olegkap wrote:

well, after execution of each command you will get the output of last executed command,

if you want to keep them in memory and analyze later then you can create new command each time

var cmd1 =

var cmd2 =

var cmd3 =

 

etc'.

 

i tried this, but with no luck actually

 

 

 Renci.SshNet.SshClient client = new Renci.SshNet.SshClient(_ip, 22, _un, _pw);
            {
                sshclient.Connect();

                var cmd = sshclient.RunCommand("echo");
                console.writeline(cmd.Result);

                var cmd1 = sshclient.RunCommand("echo1");
                 console.writeline(cmd1.Result);

                var cmd2 = sshclient.RunCommand("echo2");
                 console.writeline(cmd2.Result);
                
                sshclient.Disconnect();
            }

- is only giving me the first result - as if it hangs because the command isnt finished or something
thanks for the quick reply by the way

 

Coordinator
Sep 23, 2011 at 6:25 PM

well try this:

var cmd = sshclient.RunCommand("echo 123");
var cmd1 = sshclient.RunCommand("echo 234");
var cmd2 = sshclient.RunCommand("echo 345");

 

since I am sure there is no such command as "echo1"

Sep 23, 2011 at 6:30 PM

well im not really using echo as my commands :)

but still no results i get results if i disconnect and connect again between each command though...

Coordinator
Sep 23, 2011 at 7:55 PM

hhmm, ok then,

 

once I restore my environment I will check it out since it should work.

Sep 26, 2011 at 8:45 AM
Edited Sep 26, 2011 at 5:10 PM

this is exactly the same problem as mine... - but did you ever get a chance to look into this?

http://sshnet.codeplex.com/discussions/232663

Coordinator
Sep 28, 2011 at 1:14 PM

Yes,

 

This problem was fixed. He sent me the user/pass and I managed to fix it, as far as I remember.

 

As far as you problem, I just ran this code:

 

            using (var ssh = new SshClient(connectionInfo))
            {
                ssh.Connect();
                var cmd = ssh.RunCommand("echo 111");
                Console.WriteLine(cmd.Result);
                cmd = ssh.RunCommand("echo 222");
                Console.WriteLine(cmd.Result);
                cmd = ssh.RunCommand("echo 333");
                Console.WriteLine(cmd.Result);
                ssh.Disconnect();
            }

And I got following output:

111

222

333

So I guess I would need to know either what commands you executing and/or server version.

Peharps you can create test account for me that I can test it remotly if possible.

Feel free to contact me privately if needed.

 

Hope it helps,

 

Thanks,

Oleg

 

Sep 28, 2011 at 7:40 PM

its Cisco hardware, and ill see if i can get my home environment going again. -  it seems that its the whole connection thing again, cause it seems to close the connection after each command is executed --"isconnected = false" - which is kinda weird - seems to me like its the same for all cisco devices - but havent had to chance to figure out what differences there are in the way unix and cisco handles ssh... since cisco hardware uses unix firmware :S

Coordinator
Sep 29, 2011 at 12:41 AM

I think I saw similar problems before and unfortunately I think its cisco issue.

I think for security issues they dont like this interface too much and allow one command execution per connection.

They have its owns SSH implemention which does not have to follow general SSH rules so if this is the case then I think you have to execute one command at a time :(.

 

Let me know if you can get me an access to it and I could take a look.

 

Thanks,

Oleg

Sep 29, 2011 at 7:25 AM

yeah Cisco does like making things their own... like Apple

i wont get home until this weekend or next week, but ill set up a switch and forward it so you can get access - ill pm you the creds when its up and running

Nov 22, 2012 at 5:52 AM

Not sure if you ever got around to looking at this, but I'm having the same issue.

Sending a single command to a Cisco device works.  Sending a second command does not.  The session terminates right after the first command is issued.  I have equipment that I can give you access to, or I can just mail you a Pix / 871 to use for testing :D

 

olegkap wrote:

I think I saw similar problems before and unfortunately I think its cisco issue.

I think for security issues they dont like this interface too much and allow one command execution per connection.

They have its owns SSH implemention which does not have to follow general SSH rules so if this is the case then I think you have to execute one command at a time :(.

 

Let me know if you can get me an access to it and I could take a look.

 

Thanks,

Oleg

Mar 3, 2013 at 1:53 PM
teejmonster: I can verify this as well.

I tested with a lot of different Cisco devices. All of the ones running Native IOS drop the connection after the first command. Only newer NX-OS and IOS-XE devices (which are based on Linux) will properly keep the connection open.

The only real workaround is to code everything to use a shell connection instead.