Sample code for 30+ languages & platforms
Perl

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 Perl Downloads

Perl
use chilkat();

$success = 0;

#  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.

$ssh = chilkat::CkSsh->new();

$hostname = "ssh.example.com";
$port = 22;
$success = $ssh->Connect($hostname,$port);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

#  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.
$password = "mySshPassword";

$success = $ssh->AuthenticatePw("mySshLogin",$password);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$channelNum = $ssh->OpenSessionChannel();
if ($channelNum < 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

#  Request a pseudo-terminal.  A "dumb" terminal avoids ANSI escape sequences in the output.
$termType = "dumb";
$widthInChars = 120;
$heightInChars = 40;
$pixWidth = 0;
$pixHeight = 0;
$success = $ssh->SendReqPty($channelNum,$termType,$widthInChars,$heightInChars,$pixWidth,$pixHeight);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$success = $ssh->SendReqShell($channelNum);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

#  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->put_ReadTimeoutMs(15000);

$caseSensitive = 1;

#  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 == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

#  Read until su prompts for the password.
$success = $ssh->ChannelReceiveUntilMatch($channelNum,"Password:","utf-8",$caseSensitive);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$cmdOutput = $ssh->getReceivedText($channelNum,"utf-8");
if ($ssh->get_LastMethodSuccess() == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

print $cmdOutput . "\r\n";

#  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.
$suPassword = "myRootPassword";
$success = $ssh->ChannelSendString($channelNum,$suPassword,"utf-8");
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$success = $ssh->ChannelSendString($channelNum,"\n","utf-8");
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

#  Read the response up to the root shell prompt.  This will differ on your system.
$myShellPrompt = 'root@myserver:~#';
$success = $ssh->ChannelReceiveUntilMatch($channelNum,$myShellPrompt,"utf-8",$caseSensitive);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

#  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->get_LastMethodSuccess() == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

print $cmdOutput . "\r\n";

#  Now run a command as root.
$success = $ssh->ChannelSendString($channelNum,"ls\n","utf-8");
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$success = $ssh->ChannelReceiveUntilMatch($channelNum,$myShellPrompt,"utf-8",$caseSensitive);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$cmdOutput = $ssh->getReceivedText($channelNum,"utf-8");
if ($ssh->get_LastMethodSuccess() == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

print $cmdOutput . "\r\n";

#  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 == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$success = $ssh->ChannelSendClose($channelNum);
if ($success == 0) {
    print $ssh->lastErrorText() . "\r\n";
    exit;
}

$ssh->Disconnect();