0

I am using ssh.net to ssh to an external box, after running any command I always get "Invalid command\n" no matter what command I do write, there is not error in connection and I can see in Client.IsConnected = true and also if wrong password connection is not valid and program fails. Issue is that the commands always get the same reply from the box Command.Result = "Invalid command\n". If I login via putty, I can run "help" command without any issue. Other interesting fact is that linux command w shows what users are connected, so If I login via putty I can see user1 on, however with this little program this doesn't happen

namespace SSHconsole { class MainClass {

    public static void Main(string[] args)
    {
        //Connection information
        string user = "user1";
        string pass = "pass1";
        string host = "X.X.X.X";

        var connectionInfo = new PasswordConnectionInfo(host, user, pass);

        //Set up the SSH connection
        using (var client = new SshClient(connectionInfo))
        {

            connectionInfo.AuthenticationBanner += delegate (object sender, AuthenticationBannerEventArgs e)
            {
                Console.WriteLine(e.BannerMessage);
            };
            //Start the connection
            client.Connect();
            var command = client.RunCommand("help");
            Console.WriteLine(command.Result);
            client.Disconnect();
        }

    }
}

}

  • Can you do `plink user1@X.X.X.X help` (as **one line**, not `plink` and then `help`)? `plink` is part of PuTTY package. – Martin Prikryl Jul 06 '20 at 21:53
  • Hi Martin, thank you for your reply. I got the same reply "Invalid command" – Alejandro Castro Jul 07 '20 at 14:44
  • Thank you very much Martin, I can not give you a positive reputation as I am new user on this system or not sure how to do. If you have an easy example of sending several commands to a console, please share. – Alejandro Castro Jul 08 '20 at 21:27
  • Now that we have narrowed down the problem, you can ask more specific questions about implementing what you need using the "shell" channel. You can start with the code from my answer to: https://stackoverflow.com/q/54194139/850848 – Martin Prikryl Jul 11 '20 at 09:48
  • Hi Martin: I am a bit confused you told me to use CreateShell, but in the examples pointed there is an advise from your self as "Though note that using CreateShellStream ("shell" channel) is not the correct way to automate a commands execution. You should use CreateCommand/RunCommand ("exec" channel)". In my case CreateCommand/RunCommand, doesn't work. with CreateShell now I can send one command but in reality I am not looking for interactive session but automate. Is any other way of get in touch with you via chat or something? – Alejandro Castro Jul 12 '20 at 10:59
  • That's correct. If your SSH server was well behaving, you should use `RunCommand`. But as your server does not support the "exec" channel, you have to use `CreateShell` ("shell" channel), as you have no other option. – Martin Prikryl Jul 12 '20 at 13:14
  • Thank you very much, I think I got it working with CreateShellStream :-), also need to add sleep time between steps to automate, some tips for people who looks for the same issue, please use " sshClient.Connect(); var shell = sshClient.CreateShellStream("cli", 80, 24, 800, 600, 1024); var shellresponse = shell.ReadLine(); System.Threading.Thread.Sleep(100); shell.WriteLine(""); shellresponse = shell.Read();" – Alejandro Castro Jul 13 '20 at 21:48
  • If you need to wait for something, better than `Thread.Sleep` might be `shell.Expect`. – Martin Prikryl Jul 14 '20 at 05:18
  • Great, thank you Martin – Alejandro Castro Jul 15 '20 at 09:03
  • Hi Martin, can you please let me know how to either avoid to read "--More--" in shell.Expect or to use OR gate within Expect? – Alejandro Castro Jul 21 '20 at 17:24
  • `Expect` won't help with that. Try `terminal length 0`, see [Output of command executed with Paramiko invoke_shell() is paginated (getting “--more--” in recv)](https://stackoverflow.com/q/31999373/850848). – Martin Prikryl Jul 22 '20 at 06:27
  • Hi Martin, how to manage when Expect doesn't get what is defined? i.e: I am using something like read shell and then exp = stream.Expect("A"), then if (exp.Content("A") ==true) do something. This works fine, but when A is not in the string, the program just freezes – Alejandro Castro Jul 27 '20 at 09:32

0 Answers0