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
(Classic ASP) 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".
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% ' This example assumes the Chilkat API to have been previously unlocked. ' See Global Unlock Sample for sample code. set ssh = Server.CreateObject("Chilkat_9_5_0.Ssh") ' Hostname may be an IP address or hostname: hostname = "172.16.16.46" port = 22 success = ssh.Connect(hostname,port) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' If receiving output from a command stalls for more than 5 seconds, then stop waiting. ssh.IdleTimeoutMs = 5000 success = ssh.AuthenticatePw("myLogin","myPassword") If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Open a session channel. channelNum = ssh.OpenSessionChannel() If (channelNum < 0) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Request a pseudo-terminal termType = "dumb" widthInChars = 120 heightInChars = 40 pixWidth = 0 pixHeight = 0 success = ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Start a shell on the channel: success = ssh.SendReqShell(channelNum) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' 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.) cmd = "su" & vbLf success = ssh.ChannelSendString(channelNum,cmd,"ansi") If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Read until we get the prompt for the password: success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","ansi",1) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Display what we've received so far: Response.Write "<pre>" & Server.HTMLEncode( ssh.GetReceivedText(channelNum,"ansi")) & "</pre>" ' 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. password = "myPassword" & vbLf success = ssh.ChannelSendString(channelNum,password,"ansi") If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Read the response until we get the shell prompt (assuming it's successful) ' In my case, the shell prompt is: "chilkat@vivoMini64:~$ " myShellPrompt = "chilkat@vivoMini64:~$" ' It will be different in your case. success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"ansi",1) If (success <> 1) Then ' Check the last-error information and the session log... Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" ' Check to see what was received. Response.Write "<pre>" & Server.HTMLEncode( ssh.GetReceivedText(channelNum,"ansi")) & "</pre>" Response.End End If ' 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... Response.Write "<pre>" & Server.HTMLEncode( ssh.GetReceivedText(channelNum,"ansi")) & "</pre>" ' Send a command. In this case, we are sending the "ls" command: cmd = "ls" & vbLf success = ssh.ChannelSendString(channelNum,cmd,"ansi") If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Read until the next command prompt: success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"ansi",1) If (success <> 1) Then ' Check the last-error information and the session log... Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" ' Check to see what was received. Response.Write "<pre>" & Server.HTMLEncode( ssh.GetReceivedText(channelNum,"ansi")) & "</pre>" Response.End End If ' Display the command output: Response.Write "<pre>" & Server.HTMLEncode( ssh.GetReceivedText(channelNum,"ansi")) & "</pre>" ' 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. success = ssh.ChannelSendEof(channelNum) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Close the channel: success = ssh.ChannelSendClose(channelNum) If (success <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( ssh.LastErrorText) & "</pre>" Response.End End If ' Disconnect ssh.Disconnect %> </body> </html> |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.