Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
SSH Firewall - Login and Run Shell Commands on FirewallDemonstrates how to start a shell on a SonicWALL firewall and execute commands.
' Important: It is helpful to send the contents of the ' ssh.LastErrorText property when requesting support. Dim ssh As New Chilkat.Ssh() ' Any string automatically begins a fully-functional 30-day trial. Dim success As Boolean success = ssh.UnlockComponent("30-day trial") If (success <> true) Then MsgBox(ssh.LastErrorText) Exit Sub End If ' Connect to a firewall with SSH admin capabilities: Dim hostname As String Dim port As Long ' Hostname may be an IP address or hostname: hostname = "192.168.1.106" port = 22 ' Keep a session log, which is available via the SessionLog ' property: ssh.KeepSessionLog = true success = ssh.Connect(hostname,port) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' When reading, if no additional data arrives for more than ' 5 seconds, then abort: ssh.IdleTimeoutMs = 5000 ' SSH Server Authentication ' If there is no login/password required, you must still call ' AuthenticatePw and use any values for login/password. success = ssh.AuthenticatePw("myLogin","myPassword") If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Open a session channel. Dim channelNum As Long channelNum = ssh.OpenSessionChannel() If (channelNum < 0) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Request a pseudo-terminal Dim termType As String termType = "dumb" Dim widthInChars As Long widthInChars = 120 Dim heightInChars As Long heightInChars = 40 Dim pixWidth As Long pixWidth = 0 Dim pixHeight As Long pixHeight = 0 success = ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Start a shell on the channel: success = ssh.SendReqShell(channelNum) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' The SonicWALL firewall will send prompts for ' username and password. Read to get the first prompt: success = ssh.ChannelReceiveUntilMatch(channelNum,"User:","ansi",true) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Display what we've received so far: TextBox1.Text = TextBox1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf ' Send our firewall username. ' Note, we must end the command with a bare-LF because ' if we send a CRLF, the firewall thinks the CR is part of ' the username: success = ssh.ChannelSendString(channelNum,"myLogin\n","ansi") If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Read until we get the prompt for the password: success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","ansi",true) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Display what we've received so far: TextBox1.Text = TextBox1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf ' Send our firewall password. ' Terminate the password with a bare-LF success = ssh.ChannelSendString(channelNum,"myPassword\n","ansi") If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Assuming it's successful, we should get a command prompt. ' In this case, it is "IS-SGxxx>" success = ssh.ChannelReceiveUntilMatch(channelNum,"IS-SGxxx>","ansi",true) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub 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... TextBox1.Text = TextBox1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf ' Send a command. In this case, we are sending the "help" command: success = ssh.ChannelSendString(channelNum,"help\n","ansi") If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Read until the next command prompt: success = ssh.ChannelReceiveUntilMatch(channelNum,"IS-SGxxx>","ansi",true) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Display the command output: TextBox1.Text = TextBox1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf ' You may continue sending additional commands. ' The technique is: send the command, read until the next command prompt, ' and then empty 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 <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Close the channel: success = ssh.ChannelSendClose(channelNum) If (success <> true) Then TextBox1.Text = TextBox1.Text & ssh.LastErrorText & vbCrLf TextBox1.Text = TextBox1.Text & ssh.SessionLog & vbCrLf Exit Sub End If ' Disconnect ssh.Disconnect() |
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.