0 votes
by (520 points)
edited

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
by (144k points)
edited
 
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);
}
...