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
(Visual FoxPro) SSH -- Running Commands that Prompt for Additional Input, such as "su"Demonstrates how to run a shell command via SSH where the shell command prompts for additional input from the client. This example demonstrates "su".
LOCAL loSsh LOCAL lcHostname LOCAL lnPort LOCAL lnSuccess LOCAL lnChannelNum LOCAL lcTermType LOCAL lnWidthInChars LOCAL lnHeightInChars LOCAL lnPixWidth LOCAL lnPixHeight LOCAL lcCmd LOCAL lcPassword LOCAL lcMyShellPrompt * This example assumes the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. loSsh = CreateObject('Chilkat_9_5_0.Ssh') * Hostname may be an IP address or hostname: lcHostname = "172.16.16.46" lnPort = 22 lnSuccess = loSsh.Connect(lcHostname,lnPort) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * If receiving output from a command stalls for more than 5 seconds, then stop waiting. loSsh.IdleTimeoutMs = 5000 lnSuccess = loSsh.AuthenticatePw("myLogin","myPassword") IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Open a session channel. lnChannelNum = loSsh.OpenSessionChannel() IF (lnChannelNum < 0) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Request a pseudo-terminal lcTermType = "dumb" lnWidthInChars = 120 lnHeightInChars = 40 lnPixWidth = 0 lnPixHeight = 0 lnSuccess = loSsh.SendReqPty(lnChannelNum,lcTermType,lnWidthInChars,lnHeightInChars,lnPixWidth,lnPixHeight) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Start a shell on the channel: lnSuccess = loSsh.SendReqShell(lnChannelNum) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Send the su command. * (The SSH server I'm using for testing is a Linux Ubuntu * system running OpenSSH. It is important in this case to send a bare-LF * and not a CRLF.) lcCmd = "su" + CHR(10) lnSuccess = loSsh.ChannelSendString(lnChannelNum,lcCmd,"ansi") IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Read until we get the prompt for the password: lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"Password:","ansi",1) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Display what we've received so far: ? loSsh.GetReceivedText(lnChannelNum,"ansi") * Send the password. (This is the equivalent of typing at the shell prompt.) * Again, make sure it uses a bare-LF and not a CRLF. lcPassword = "myPassword" + CHR(10) lnSuccess = loSsh.ChannelSendString(lnChannelNum,lcPassword,"ansi") IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Read the response until we get the shell prompt (assuming it's successful) * In my case, the shell prompt is: "chilkat@vivoMini64:~$ " lcMyShellPrompt = "chilkat@vivoMini64:~$" * It will be different in your case. lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,lcMyShellPrompt,"ansi",1) IF (lnSuccess <> 1) THEN * Check the last-error information and the session log... ? loSsh.LastErrorText * Check to see what was received. ? loSsh.GetReceivedText(lnChannelNum,"ansi") RELEASE loSsh CANCEL ENDIF * Display what we've received so far. This clears * the internal receive buffer, which is important. * After we send the command, we'll be reading until * the next command prompt. If the command prompt * is already in the internal receive buffer, we'll think we're * already finished... ? loSsh.GetReceivedText(lnChannelNum,"ansi") * Send a command. In this case, we are sending the "ls" command: lcCmd = "ls" + CHR(10) lnSuccess = loSsh.ChannelSendString(lnChannelNum,lcCmd,"ansi") IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Read until the next command prompt: lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,lcMyShellPrompt,"ansi",1) IF (lnSuccess <> 1) THEN * Check the last-error information and the session log... ? loSsh.LastErrorText * Check to see what was received. ? loSsh.GetReceivedText(lnChannelNum,"ansi") RELEASE loSsh CANCEL ENDIF * Display the command output: ? loSsh.GetReceivedText(lnChannelNum,"ansi") * You may continue sending additional commands. * The technique is: send the command, read until the next command prompt, * and then fetch/clear the internal receive buffer. * We're done, so shut it down.. * Send an EOF. This tells the server that no more data will * be sent on this channel. The channel remains open, and * the SSH client may still receive output on this channel. lnSuccess = loSsh.ChannelSendEof(lnChannelNum) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Close the channel: lnSuccess = loSsh.ChannelSendClose(lnChannelNum) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText RELEASE loSsh CANCEL ENDIF * Disconnect loSsh.Disconnect() RELEASE loSsh |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.