VBScript
VBScript
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
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.
set ssh = CreateObject("Chilkat.Ssh")
hostname = "ssh.example.com"
port = 22
success = ssh.Connect(hostname,port)
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
' 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) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
channelNum = ssh.OpenSessionChannel()
If (channelNum < 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
' 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) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
success = ssh.SendReqShell(channelNum)
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
' 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
caseSensitive = 1
' Send the su command. Linux/UNIX servers generally expect a bare LF, not a CRLF.
success = ssh.ChannelSendString(channelNum,"su" & vbLf,"utf-8")
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
' Read until su prompts for the password.
success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","utf-8",caseSensitive)
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
cmdOutput = ssh.GetReceivedText(channelNum,"utf-8")
If (ssh.LastMethodSuccess = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
outFile.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.
suPassword = "myRootPassword"
success = ssh.ChannelSendString(channelNum,suPassword,"utf-8")
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
success = ssh.ChannelSendString(channelNum,vbLf,"utf-8")
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
' 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) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
' 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 = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
outFile.WriteLine(cmdOutput)
' Now run a command as root.
success = ssh.ChannelSendString(channelNum,"ls" & vbLf,"utf-8")
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"utf-8",caseSensitive)
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
cmdOutput = ssh.GetReceivedText(channelNum,"utf-8")
If (ssh.LastMethodSuccess = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
outFile.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 = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
success = ssh.ChannelSendClose(channelNum)
If (success = 0) Then
outFile.WriteLine(ssh.LastErrorText)
WScript.Quit
End If
ssh.Disconnect
outFile.Close