Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
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 lnSuccess LOCAL lcHostname LOCAL lnPort LOCAL lnChannelNum LOCAL lcTermType LOCAL lnWidthInChars LOCAL lnHeightInChars LOCAL lnPixWidth LOCAL lnPixHeight LOCAL lcCmd LOCAL lcPassword * Important: It is helpful to send the contents of the * ssh.LastErrorText property when requesting support. loSsh = CreateObject('Chilkat.Ssh') * Any string automatically begins a fully-functional 30-day trial. lnSuccess = loSsh.UnlockComponent("30-day trial") IF (lnSuccess <> 1) THEN =MESSAGEBOX(loSsh.LastErrorText) QUIT ENDIF * Hostname may be an IP address or hostname: lcHostname = "192.168.1.117" lnPort = 22 * Keep a session log, which is available via the SessionLog * property: loSsh.KeepSessionLog = 1 lnSuccess = loSsh.Connect(lcHostname,lnPort) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText ? loSsh.SessionLog QUIT ENDIF * When reading, if no additional data arrives for more than * 5 seconds, then abort: loSsh.IdleTimeoutMs = 5000 * SSH Server Authentication * If there is no login/password required, you must still call * AuthenticatePw and use any values for login/password. lnSuccess = loSsh.AuthenticatePw("chilkat","myPassword") IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText ? loSsh.SessionLog QUIT ENDIF * Open a session channel. lnChannelNum = loSsh.OpenSessionChannel() IF (lnChannelNum < 0) THEN ? loSsh.LastErrorText ? loSsh.SessionLog QUIT 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 ? loSsh.SessionLog QUIT ENDIF * Start a shell on the channel: lnSuccess = loSsh.SendReqShell(lnChannelNum) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText ? loSsh.SessionLog QUIT 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 ? loSsh.SessionLog QUIT ENDIF * Read until we get the prompt for the password: lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"Password:","ansi",1) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText ? loSsh.SessionLog QUIT ENDIF * Display what we've received so far: ? loSsh.GetReceivedText(lnChannelNum,"ansi") * Send the password. * 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 ? loSsh.SessionLog QUIT ENDIF * Read the response until we get the shell prompt (assuming it's successful) * In my case, the shell prompt is: "root@ubuntu:/home/chilkat# " * It will be different in your case. lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"root@ubuntu:/home/chilkat#","ansi",1) IF (lnSuccess <> 1) THEN * Check the last-error information and the session log... ? loSsh.LastErrorText ? loSsh.SessionLog * Check to see what was received. ? loSsh.GetReceivedText(lnChannelNum,"ansi") QUIT 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 ? loSsh.SessionLog QUIT ENDIF * Read until the next command prompt: lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"root@ubuntu:/home/chilkat#","ansi",1) IF (lnSuccess <> 1) THEN * Check the last-error information and the session log... ? loSsh.LastErrorText ? loSsh.SessionLog * Check to see what was received. ? loSsh.GetReceivedText(lnChannelNum,"ansi") QUIT 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 ? loSsh.SessionLog QUIT ENDIF * Close the channel: lnSuccess = loSsh.ChannelSendClose(lnChannelNum) IF (lnSuccess <> 1) THEN ? loSsh.LastErrorText ? loSsh.SessionLog QUIT ENDIF * Disconnect loSsh.Disconnect() |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.
Mail Component · .NET Email Component · ASP Mail Component · XML Parser