Rebex Shell SendCommand always throws "Cannot send command while running."

0 votes
asked Jul 2, 2012 by walljm (520 points)
edited Jul 3, 2012

here is the code i'm using. is this a bug or am i doing something wrong? It errors out at "sh.SendCommand("cd /");"

Rebex.Net.Ssh ssh = new Rebex.Net.Ssh();
ssh.Connect("domain.net");
Rebex.Net.SshPrivateKey key = new Rebex.Net.SshPrivateKey(@"private.ppk", "");
ssh.Login("username", key);

Shell sh = ssh.StartCommand("pwd");

// read all response line-by-line
while (true)
{
    // read a single response line
    string line = sh.ReadLine();

    // a value of null indicates end of response
    if (line == null)
        break;

    // display the line
    Console.WriteLine(line);
}

sh.SendCommand("cd /");

while (true)
{
    // read a single response line
    string line = sh.ReadLine();

    // a value of null indicates end of response
    if (line == null)
        break;

    // display the line
    Console.WriteLine(line);
}

1 Answer

0 votes
answered Jul 3, 2012 by Lukas Pokorny (121,750 points)
edited Jul 3, 2012
 
Best answer

Shell objects started with Ssh.StartCommand don't support running multiple commands. To do that, either call Ssh.StartCommand multiple times (if the commands are independent on each other) or call Ssh.StartShell instead:

Rebex.Net.Ssh ssh = new Rebex.Net.Ssh();
ssh.Connect("domain.net");
Rebex.Net.SshPrivateKey key = new Rebex.Net.SshPrivateKey(@"private.ppk", "");
ssh.Login("username", key);

// start a shell
Shell sh = ssh.StartShell(ShellMode.WellKnownShell);

sh.SendCommand("pwd");

// read all response line-by-line
while (true)
{
    // read a single response line
    string line = sh.ReadLine();

    // a value of null indicates end of response
    if (line == null)
        break;

    // display the line
    Console.WriteLine(line);
}

sh.SendCommand("cd /");

while (true)
{
    // read a single response line
    string line = sh.ReadLine();

    // a value of null indicates end of response
    if (line == null)
        break;

    // display the line
    Console.WriteLine(line);
}
...