Visual Basic Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CUnicode C++Unicode CMFCDelphi DLLDelphi ActiveXFoxProJavaPerlPHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

VB Examples

Bounced Mail
Bz2
Character Encoding
CSV
Digital Certificates
Digital Signatures
Email
FTP
HTML Conversion
HTTP
IMAP
Encryption
MHT / HTML Email
POP3
RSA
S/MIME
SFTP
SMTP
Socket
Spider
SSH
SSH Key
SSH Tunnel
String
Tar
Upload
XML
XMP
Zip Compression

More Examples...
Amazon S3
Email Object
DKIM / DomainKey
NTLM
DH Key Exchange
DSA
FileAccess
RSS
Atom
Self-Extractor
Service
Bzip2
PPMD
Deflate
LZW


VB Strings
VB Byte Array

 

 

 

 

 

 

 

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".

Download Chilkat 32-bit SSH / SFTP ActiveX (.msi)

Download All 32-bit Chilkat ActiveX Components (.zip)

Download All 64-bit Chilkat ActiveX Components (.zip)

'  Important: It is helpful to send the contents of the
'  ssh.LastErrorText property when requesting support.

Dim ssh As New ChilkatSsh

'  Any string automatically begins a fully-functional 30-day trial.
Dim success As Long
success = ssh.UnlockComponent("30-day trial")
If (success <> 1) Then
    MsgBox ssh.LastErrorText
    Exit Sub
End If

Dim hostname As String
Dim port As Long

'  Hostname may be an IP address or hostname:
hostname = "192.168.1.117"
port = 22

'  Keep a session log, which is available via the SessionLog
'  property:
ssh.KeepSessionLog = 1

success = ssh.Connect(hostname,port)
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.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("chilkat","myPassword")
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Open a session channel.
Dim channelNum As Long
channelNum = ssh.OpenSessionChannel()
If (channelNum < 0) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.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 <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Start a shell on the channel:
success = ssh.SendReqShell(channelNum)
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
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.)
Dim cmd As String
cmd = "su" & vbLf
success = ssh.ChannelSendString(channelNum,cmd,"ansi")
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Read until we get the prompt for the password:
success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","ansi",1)
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Display what we've received so far:
Text1.Text = Text1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf

'  Send the password.
'  Again, make sure it uses a bare-LF and not a CRLF.
Dim password As String
password = "myPassword" & vbLf
success = ssh.ChannelSendString(channelNum,password,"ansi")
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  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.
success = ssh.ChannelReceiveUntilMatch(channelNum,"root@ubuntu:/home/chilkat#","ansi",1)
If (success <> 1) Then
    '  Check the last-error information and the session log...
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    '  Check to see what was received.
    Text1.Text = Text1.Text & ssh.GetReceivedText(channelNum,"ansi") & 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...
Text1.Text = Text1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf

'  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
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Read until the next command prompt:
success = ssh.ChannelReceiveUntilMatch(channelNum,"root@ubuntu:/home/chilkat#","ansi",1)
If (success <> 1) Then
    '  Check the last-error information and the session log...
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    '  Check to see what was received.
    Text1.Text = Text1.Text & ssh.GetReceivedText(channelNum,"ansi") & vbCrLf
    Exit Sub
End If

'  Display the command output:
Text1.Text = Text1.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 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
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Close the channel:
success = ssh.ChannelSendClose(channelNum)
If (success <> 1) Then
    Text1.Text = Text1.Text & ssh.LastErrorText & vbCrLf
    Text1.Text = Text1.Text & ssh.SessionLog & vbCrLf
    Exit Sub
End If

'  Disconnect
ssh.Disconnect 

© 2000-2013 Chilkat Software, Inc. All Rights Reserved.