6

I'm using C# with SSH.NET and have been able to get a client SSH connection working fine to executing commands across SSH. I can connect to a Linux install I have on a hypervisor VM with no issues, read back the StdOut and StdErr, etc.

However, each command is like a completely new session - it always starts from /home/user, even if you finish from another directory with the last command. You can string multiple commands together with semicolons:

pwd; cd ..; pwd
/home/user
/home

but then if I execute:

pwd
/home/user

I'm back at the home directory for that user.

I'm not disconnecting and closing the session between the commands. Here is some example code for several commands in a row, using changes in path to illustrate the issue:

public void MultiCommandTest()
{
    string output1, output2;

    var host = Config.SshHostName;
    var port = Config.SshHostPort;
    var user = Config.SshUsername;
    var pass = Config.SshPassword;
    var auth = new PasswordAuthenticationMethod(user, pass);
    var info = new ConnectionInfo(host,port,user,auth);

    using (var ssh = new SshClient(info))
    {
        ssh.Connect();
        var cmd1 = ssh.CreateCommand("pwd; cd ..; pwd");
        output1 = cmd1.Execute();
        var cmd2 = ssh.CreateCommand("pwd; cd ..; pwd");
        output2 = cmd2.Execute();
        ssh.Disconnect();

    }

    string output = output1 + Environment.NewLine + output2;
    System.Windows.Forms.Clipboard.SetText(output);
    System.Windows.Forms.MessageBox.Show(output);
}

I realize there is a difference between an interactive and non-interactive session when connecting to Bash, etc., but don't know enough about linux, etc. to know if that could be part of the solution (i.e. maybe it has to be resolved in the login side with .profile, .bashrc, or similar?).

Question:

So - is there a way to have a shell-like connection with SSH (and SSH.NET in particular)? Or do I just need to serialize any sets of commands and send them all at once, always starting from scratch for any new command?

0

2 Answers 2

7

You can implement a shell session using:

  • SshClient.CreateShellStream (ShellStream class) — It gives you one Stream interface, that you can both write and read. This interface is useful, when you want to interact with the shell directly.
  • or SshClient.CreateShell (Shell class) — Where you yourself provide separate streams for input, output and error output. This interface is useful, when you want to link/pipe the input and output to existing streams (like standard input and output or files). Though you can still interact directly, if you use PipeStream. See also SSH.NET doesn't process my shell input commands.

But using "shell" is really not recommended for automating commands execution. A shell is an interactive feature, that does not have a deterministic behavior.

You better use an exec channel, see How to run commands on SSH server in C#?

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

3 Comments

Have a new reason for this - I have a debug console program that I need to interface with that does not accept stdin/stdout, so I have to interact with it as an interactive console, even for automation. I might be able to get that updated, but for the moment, I am working to implement SSH.NET as a stream to accomplish this goal.
Interactive console is not a shell. Even with "exec" channel, you can use an interactive console. But SSH.NET does not support it. You would have to modify SSH.NET code to issue SendPseudoTerminalRequest request in SshCommand.Execute implementation (I didn't test this). See Shell.Start() for an example.
Thank you for this info, that's more what I was intending to ask about with the original question. I'll head down this path and see where it leads.
0

For the record, this was resolved by using SSH to connect and open a tunnel, then using telnet to do the interactive part. SSH.NET was not mature enough on the interactive side.

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.