Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Network Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Network/SSH/SSH Terminal
This example is the version from Fri, 8th Oct 2020.
Project "SSH Terminal.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
End EventHandler
End Class
Class MainWindow Inherits Window
Control Output Inherits TextArea
ControlInstance Output Inherits TextArea
End Control
Control InputField Inherits TextField
ControlInstance InputField Inherits TextField
EventHandler Function KeyDown(Key As String) As Boolean
If Asc(key) = 13 Or Asc(key) = 3 Then
Dim I As String = InputField.Text.ConvertEncoding(encodings.UTF8) + EndOfLine.UNIX
Dim n As Integer = channel.Write(I)
// show in text area
Output.SelBold = True
Output.AppendText InputField.Text
Output.AppendText EndOfLine
Output.SelBold = False
If channel.LastError <> 0 Then
Print "Write reports error: "+Str(channel.LastError)
End If
InputField.Text = ""
Output.ScrollPosition = 999999 // jump to end
End If
End EventHandler
End Control
Control HelpLabel Inherits Label
ControlInstance HelpLabel Inherits Label
End Control
EventHandler Sub Close()
channel.Close
channel = Nil
session.Disconnect "Normal Shutdown, Thank you for playing"
End EventHandler
EventHandler Sub Open()
Const Address = "localhost"
Const username = "xxx"
Const password = "yyy"
Const keyfile1 = ""
Const keyfile2 = ""
Const commandline = "/bin/sh"
Break // change values above!
Dim hostAddr As String = System.Network.LookupIPAddress(Address)
Print "Connect to IP: "+hostAddr
//* Create a session instance and start it up. This will trade welcome
//* banners, exchange keys, and setup crypto, compression, and MAC layers
session = New MySSH2SessionMBS(hostAddr, 22)
session.MyPassword = password
If session.Handle = 0 Then
Print "Failed to connect socket!"
Return
End If
session.SessionHandshake
If session.lasterror<>0 Then
Print "Failure establishing SSH session"
Return
End If
//* At this point we havn't authenticated. The first thing to do is check
//* the hostkey's fingerprint against our known hosts Your app may have it
//* hard coded, may go to a file, may present it to the user, that's your
//* call
Dim fingerprint As String = session.HostKeyHash(session.kHostKeyHashSHA1)
Print "Fingerprint: "+EncodeHex(fingerprint)
//* check what authentication methods are available */
Dim userauthlist As String = session.UserAuthList(username)
Dim authPassword As Boolean = False
Dim authKeyboardInteractive As Boolean = False
Dim authPublickey As Boolean = False
Print "Authentication methods: "+ userauthlist
If InStr(userauthlist, "password")>0 Then
authPassword = True
End If
If InStr(userauthlist, "keyboard-interactive")>0 Then
authKeyboardInteractive = True
End If
If InStr(userauthlist, "publickey")>0 Then
authPublickey = True
End If
If authPassword Then
//* We could authenticate via password */
session.UserAuthPassword(username, password)
If session.LastError = 0 Then
// ok
Print "Authentication by password succeeded."
Else
// failed
Print "Authentication by password failed!"
Return
End If
Elseif authKeyboardInteractive Then
//* Or via keyboard-interactive */
session.UserAuthKeyboardInteractive username
If session.LastError <> 0 Then
Print "Authentication by keyboard-interactive failed!"
Return
Else
Print "Authentication by keyboard-interactive succeeded."
End If
//* Or by public key */
Elseif authPublickey Then
session.UserAuthPublicKeyFromFile(username, keyfile1, keyfile2, password)
If session.LastError <> 0 Then
Print "Authentication by public key failed!"
Return
Else
Print "Authentication by public key succeeded."
End If
Else
Print "No supported authentication methods found!"
Return
End If
//* Request a shell */
channel = new MySSH2ChannelMBS(session)
channel.SetBlocking False
app.DoEvents 10
// read greetings
Dim s As String = channel.Read(10000)
Print s
// execute command line
app.DoEvents 10
channel.execute commandline
If channel.LastError <> 0 and channel.LastError <> -37 Then
Print "Execute failed with error: "+Str(channel.LastError)
End If
// now continue with DataAvailable Handler
End EventHandler
Sub Print(s as string, isError as Boolean = false)
If isError Then
// red color for errors
output.SelTextColor = &c990000
end if
output.AppendText s
Output.AppendText EndOfLine
If isError Then
output.SelTextColor = TextColor
End If
Output.ScrollPosition = 999999 // jump to end
End Sub
Property channel As MySSH2ChannelMBS
Property session As MySSH2SessionMBS
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MySSH2SessionMBS Inherits SSH2SessionMBS
EventHandler Sub KeyboardCallback(Name as string, Instruction as string, PromptCount as integer, Prompts() as SSH2UserAuthKeyboardInteractivePromptMBS, responses() as SSH2UserAuthKeyboardInteractiveResponseMBS)
print "Name: "+Name
Print "Instruction: "+Instruction
for each p as SSH2UserAuthKeyboardInteractivePromptMBS in Prompts
print p.Text
next
if PromptCount = 1 then
responses(0).Text = MyPassword
end if
End EventHandler
Sub Print(s as string)
MainWindow.Print s
End Sub
Property MyPassword As string
End Class
Class MySSH2ChannelMBS Inherits SSH2ChannelMBS
EventHandler Sub DataAvailable(Data as string, ErrorChannel as boolean)
// ok
MainWindow.Print data, ErrorChannel
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Network Plugin.