Question

I'm using SSH.NET to connect (with username + password) to a server and run some commands. Connection to server is made to manage some components via Cisco Application Control Software, but that shouldn't matter because command are sent as they are.

Connection to server works well, all other commands work too but two specific commands doesn't work, and I can't seem to understand why.
The commands that fail are: changeto Production and terminal length 0.
My code is pretty simple (simplified for the question, but same flow):

using (var client = new SshClient(serverIP, port, username, password)
{
    client.Connect();
    var cmd = Client.RunCommand("terminal length 0");
    cmd = client.RunCommand("changeto Production");
    // Other command follow
}

While running the same commands via putty it works, and no, changeto shouldn't be change to.
I think the problem here is changing scopes (changeto Production) via code, but can't understand why.

Any help will be appreciated.

Was it helpful?

Solution

After searching and asking on the CodePlex forum of the SSH.NET library, I understood the problem: the RunCommand() function creates a new shell every call.
The new shell is created with default environment, and because terminal length 0 and changeto Production change the runtime-environment for following commands, this can't be accomplished with RunCommand().
I ended up writing and reading directly from the shell stream (using SshClient.CreateShellStream()) and playing with Expect() to get and parse the output I need. This way all commands are executed in the same shell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top