C#
C#
SSH Commands that Prompt for Input, such as su
See more SSH Examples
Demonstrates running a shell command that prompts for additional input, using su as the example. The technique is: send the command, read until the expected prompt appears, then send the response exactly as though it were typed.
Note: This example deliberately requests a PTY. su reads its password from a controlling terminal and refuses to run without one, so a pseudo-terminal is genuinely required here.
Background: Programs that ask for a password almost always insist on reading it from a real terminal rather than a pipe — a deliberate safeguard against passwords being fed in by scripts. That is why this is one of the cases where a PTY is necessary despite the general advice to avoid one. The interaction is a strict send-then-read-to-prompt cycle, and retrieving the received text between steps is essential: a prompt left in the buffer would make the next receive return immediately with the wrong output. Where possible, a passwordless
sudo rule avoids this dance altogether.Chilkat C# Downloads
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running a shell command that prompts for additional input, using "su" as the
// example. The technique is: send the command, read until the expected prompt, then send the
// response as though it were typed.
//
// Note: This example deliberately DOES request a PTY. "su" reads its password from a
// controlling terminal and refuses to run without one, so a pseudo-terminal is required here.
Chilkat.Ssh ssh = new Chilkat.Ssh();
string hostname = "ssh.example.com";
int port = 22;
success = ssh.Connect(hostname,port);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
string password = "mySshPassword";
success = ssh.AuthenticatePw("mySshLogin",password);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
int channelNum = ssh.OpenSessionChannel();
if (channelNum < 0) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
// Request a pseudo-terminal. A "dumb" terminal avoids ANSI escape sequences in the output.
string termType = "dumb";
int widthInChars = 120;
int heightInChars = 40;
int pixWidth = 0;
int pixHeight = 0;
success = ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
success = ssh.SendReqShell(channelNum);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
// IMPORTANT: Set a read timeout before receiving until a match. ReadTimeoutMs defaults to 0,
// which means no limit -- without it, this call waits forever if the received data never
// contains a match.
ssh.ReadTimeoutMs = 15000;
bool caseSensitive = true;
// Send the su command. Linux/UNIX servers generally expect a bare LF, not a CRLF.
success = ssh.ChannelSendString(channelNum,"su\n","utf-8");
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
// Read until su prompts for the password.
success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","utf-8",caseSensitive);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
string cmdOutput = ssh.GetReceivedText(channelNum,"utf-8");
if (ssh.LastMethodSuccess == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
Debug.WriteLine(cmdOutput);
// Send the root password, exactly as if it were typed at the prompt. Like the login password,
// this should come from a secure source rather than being hard-coded.
string suPassword = "myRootPassword";
success = ssh.ChannelSendString(channelNum,suPassword,"utf-8");
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
success = ssh.ChannelSendString(channelNum,"\n","utf-8");
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
// Read the response up to the root shell prompt. This will differ on your system.
string myShellPrompt = "root@myserver:~#";
success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"utf-8",caseSensitive);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
// Retrieving the text also clears the receive buffer, which matters: if the prompt were left
// buffered, the next receive would match it immediately.
cmdOutput = ssh.GetReceivedText(channelNum,"utf-8");
if (ssh.LastMethodSuccess == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
Debug.WriteLine(cmdOutput);
// Now run a command as root.
success = ssh.ChannelSendString(channelNum,"ls\n","utf-8");
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"utf-8",caseSensitive);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
cmdOutput = ssh.GetReceivedText(channelNum,"utf-8");
if (ssh.LastMethodSuccess == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
Debug.WriteLine(cmdOutput);
// Additional commands follow the same pattern: send the command, read to the next prompt, then
// fetch and clear the receive buffer.
success = ssh.ChannelSendEof(channelNum);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
success = ssh.ChannelSendClose(channelNum);
if (success == false) {
Debug.WriteLine(ssh.LastErrorText);
return;
}
ssh.Disconnect();