1

I'm looking to automate password change that's accomplished manually as such:

plink -ssh user@host changepass
user@host's password:
Access granted. Press Return to begin session.
Enter current password:
...

My understanding is that I am unable to accomplish this using RunCommand because it's not interactive and will not allow me to further specify the current password, hit enter, then the new password, etc. From what I understand I would have to use CreateShellStream to emulate a shell. When I try to do something like this:

using (var client = new SshClient(host, username, password))
{
    client.Connect();

    ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
    Console.Write(SendCommand("changepass", shellStream));
    
    client.Disconnect();
}

The result is The command executed is invalid..... which is the same thing that I get if I attempt to use plink without specifying the command inline:

plink -ssh -t user@host
Using username "user".
-- Pre-authentication banner message from server: ----------------------------
user@host's password:
Access granted. Press Return to begin session.
The command executed is invalid.....

I am wondering if my approach is invalid and whether this can be accomplished with SSH.NET. If not what would be a good way to do this from C#?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
e36M3
  • 5,952
  • 6
  • 36
  • 47

1 Answers1

0

Your server does not seem to support the "shell" channel, only the "exec" channel.

Indeed SSH.NET does not support providing an input to a command executed with the "exec" channel. See Providing input/subcommands to a command (cli) executed with SSH.NET SshClient.RunCommand

Were it a *nix server, you might try to provide the input using input redirection.

client.RunCommand("echo input | command");

Though 1) this is not a safe way for passwords 2) I do not know a proper syntax for z/OS (or if it is even possible).


To allow providing the input properly, you would have to grab SSH.NET code and modify the SshCommand class to expose IChannelSession.Write the way ShellStream.Write does.


Or you will have to use a different library (I do not know other free good one than SSH.NET though). Or run an external tool like PuTTY Plink (I do not like that, but it's probably easiest way to go). See Sending input during command output with SSH.NET.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992