Home > Enterprise >  ssh.net how to pass 'remote command' when connecting to ssh server?
ssh.net how to pass 'remote command' when connecting to ssh server?

Time:12-14

i have some solution based on ssh.net library now it department watn to monitor ssh sessions so they ask us to connect to some like 'proxy' ssh server and pass remote command to them to be like redirrected to destination machine

so basycly quastion is how to send this parameter enter image description here using ssh.net ?

so i have try like

var pk = new PrivateKeyFile("C:\\xxx.ppk");        
client = new SshClient("x.x.x.x","login",pk);
client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);
client.Connect();
SshCommand x = client.CreateCommand("user@server1");
var response = x.Execute();
shells = client.CreateShellStream("vt100", 250, 250, 800, 160, 512);

but when running it like this i get empty response but error of this command is

Error = "Pseudo-terminal will not be allocated because stdin is not a terminal.\r\nPermission denied, please try again.\r\nPermission denied, please try again.\r\nReceived disconnect from x.x.x.x port 22:2: Too many authentication failures for y\r\nDisconne...

if i do it from putty and send this user@server1 in 'remote command' then i get imidietly asked for password for passed user @ server1

please help a bit if someone known ;) thanks and regards !

CodePudding user response:

The underling API of PuTTY "Remote command" is SSH "exec" channel.

In SSH.NET the API for "exec" channel is indeed the SshClient.CreateCommand/SshClient.RunCommand:
How to run commands on SSH server in C#?

But while PuTTY (being interactive SSH terminal) allocates pseudo terminal for the session by default, the SSH.NET SshClient.CreateCommand (being intended for command automation and not for an interactive use) does not. Your command seems to be intended for an interactive use, so it cannot run in such session.

  • Check if you command has an option/switch/whatever to make it run in a session without pseudo terminal.
  • If there's no such option, you will have to implement alternative to the standard SshClient.CreateCommand/SshClient.RunCommand that allocates the pseudo-terminal by calling ChannelSession.SendPseudoTerminalRequest.
  • Related