2

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.

2
  • is changeto an alias sourced in login shell profile? have you tried to source your ~/.bash_profile? Commented Jul 18, 2013 at 9:14
  • What do you mean? Where is that file located? When I run this command (via code) it doesn't produce any error, it just acts like command was executed successfully but the action doesn't happen. Via putty it works (without any files as far as I know, I downloaded it just for test purpose). Commented Jul 18, 2013 at 10:23

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.