Xojo Plugin
Xojo Plugin
Using sudo in an SSH Shell Session
See more SSH Examples
Demonstrates running a command with sudo in a shell session without an interactive prompt. sudo -S reads the password from stdin and -p "" suppresses the prompt, so piping the password in with echo runs the command as root with no interaction.
Security note: The password appears in the command line, so it may be visible in the remote host's process list and shell history. A passwordless sudo rule for the specific command is preferable where it can be arranged.
Background: This is the scripted alternative to the interactive
su approach: rather than waiting for a password prompt and answering it, the password is supplied up front so no terminal interaction is needed. Because QuickShell allocates a PTY, the shell echoes back everything sent to it, which is what motivates the quoting trick on the final marker — writing echo THIS 'IS' THE END means the echoed command line contains the quotes while the real output does not, so matching the unquoted text matches actual output rather than the echo.Chilkat Xojo Plugin Downloads
Dim success As Boolean
success = False
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates running a command with "sudo" in a shell session, supplying the password without
// an interactive prompt.
Dim ssh As New Chilkat.Ssh
Dim port As Int32
port = 22
success = ssh.Connect("ssh.example.com",port)
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
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.
Dim password As String
password = "mySshPassword"
success = ssh.AuthenticatePw("mySshLogin",password)
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
// Start a shell session. QuickShell allocates a PTY, so the shell echoes the commands sent
// to it and prints a prompt.
Dim channelNum As Int32
channelNum = ssh.QuickShell()
If (channelNum < 0) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
// Build the command. "sudo -S" makes sudo read the password from stdin, and "-p" sets the
// prompt -- setting it to an empty string removes the prompt entirely. Piping the password in
// via "echo" therefore runs the command as root with no interaction.
//
// Security note: the password appears in the command line, so it can be visible in the remote
// host's process list and shell history. Prefer a passwordless sudo rule where possible.
Dim sbCommands As New Chilkat.StringBuilder
success = sbCommands.Append("echo """)
success = sbCommands.Append(password)
success = sbCommands.Append(""" | sudo -S -p """" ls" + EndOfLine.Unix)
// The final command echoes a marker used to detect the end of the output. The single quotes
// around 'IS' are a trick: the terminal echo of the typed command includes the quotes, while
// the command's actual output does not. Matching the unquoted form therefore matches the real
// output rather than the echo.
success = sbCommands.Append("echo THIS 'IS' THE END OF THE SCRIPT" + EndOfLine.Unix)
Dim commands As String
commands = sbCommands.GetAsString()
success = ssh.ChannelSendString(channelNum,commands,"utf-8")
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
// No more commands will be sent.
success = ssh.ChannelSendEof(channelNum)
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
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
Dim caseSensitive As Boolean
caseSensitive = True
success = ssh.ChannelReceiveUntilMatch(channelNum,"THIS IS THE END OF THE SCRIPT","utf-8",caseSensitive)
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
// Close the channel only after the desired output has been received.
success = ssh.ChannelSendClose(channelNum)
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
success = ssh.ChannelReceiveToClose(channelNum)
If (success = False) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
Dim sessionOutput As String
sessionOutput = ssh.GetReceivedText(channelNum,"utf-8")
If (ssh.LastMethodSuccess = False) Then
System.DebugLog(ssh.LastErrorText)
Return
End If
System.DebugLog("--- output ----")
System.DebugLog(sessionOutput)
ssh.Disconnect