Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Using sudo in an SSH Shell SessionSee more SSH ExamplesDemonstrates how to run a command as sudo in a shell session.
integer li_rc oleobject loo_Ssh integer li_Port integer li_Success integer li_ChannelNum oleobject loo_SbCommands // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Ssh = create oleobject // Use "Chilkat_9_5_0.Ssh" for versions of Chilkat < 10.0.0 li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh") if li_rc < 0 then destroy loo_Ssh MessageBox("Error","Connecting to COM object failed") return end if li_Port = 22 li_Success = loo_Ssh.Connect("the-ssh-server.com",li_Port) if li_Success <> 1 then Write-Debug loo_Ssh.LastErrorText destroy loo_Ssh return end if // Authenticate using login/password: li_Success = loo_Ssh.AuthenticatePw("theSshLogin","theSshPassword") if li_Success <> 1 then Write-Debug loo_Ssh.LastErrorText destroy loo_Ssh return end if // Start a shell session. // (The QuickShell method was added in Chilkat v9.5.0.65) li_ChannelNum = loo_Ssh.QuickShell() if li_ChannelNum < 0 then Write-Debug loo_Ssh.LastErrorText destroy loo_Ssh return end if // Construct a StringBuilder with our command ("ls") run with "sudo" // Note: The line-endings are potentially important. Some SSH servers may // require either LF or CRLF line endings. (Unix/Linux/OSX servers typically // use bare-LF line endings. Windows servers likely use CRLF line endings.) // Using "sudo -S" causes the sudo prompt to be written to stderr, // and the password is read from stdin (i.e. from the echo command ouput). // The sudo commands -p option allows us to set the prompt, and we can set it to // the empty string, thus eliminating the prompt from the stderr completely. // This allows us to run the command as root (super-user) with no prompt. loo_SbCommands = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbCommands.ConnectToNewObject("Chilkat.StringBuilder") loo_SbCommands.Append("echo ~"theSshPassword~" | sudo -S -p ~"~" ls~n") // Add another command to echo a marker string that // we'll use in ChannelReceiveUntilMatch below. // The use of single quotes around 'IS' is a trick so that the output // of the command is "THIS IS THE END OF THE SCRIPT", but the terminal echo // includes the single quotes. This allows us to read until we see the actual // output of the last command. loo_SbCommands.Append("echo THIS 'IS' THE END OF THE SCRIPT~n") // Send the commands.. li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,loo_SbCommands.GetAsString(),"ansi") if li_Success <> 1 then Write-Debug loo_Ssh.LastErrorText destroy loo_Ssh destroy loo_SbCommands return end if // Send an EOF to indicate no more commands will be sent. // For brevity, we're not checking the return values of each method call. // Your code should check the success/failure of each call. li_Success = loo_Ssh.ChannelSendEof(li_ChannelNum) // Receive output up to our marker. li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"THIS IS THE END OF THE SCRIPT","ansi",1) // Close the channel. // It is important to close the channel only after receiving the desired output. li_Success = loo_Ssh.ChannelSendClose(li_ChannelNum) // Get any remaining output.. li_Success = loo_Ssh.ChannelReceiveToClose(li_ChannelNum) // Get the complete output for all the commands in the session. Write-Debug "--- output ----" Write-Debug loo_Ssh.GetReceivedText(li_ChannelNum,"ansi") destroy loo_Ssh destroy loo_SbCommands |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.