Sample code for 30+ languages & platforms
AutoIt

SSH Commands to Sophos Router Device Console

See more SSH Examples

Demonstrates how to establish an SSH session with a Sophos XG router and send commands in a device console session.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.

$oSsh = ObjCreate("Chilkat.Ssh")

$bSuccess = $oSsh.Connect("172.16.16.16",22)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Authenticate using login/password:
$bSuccess = $oSsh.AuthenticatePw("myLogin","myPassword")
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Start a shell session.
; (The QuickShell method was added in Chilkat v9.5.0.65)
Local $iChannelNum = $oSsh.QuickShell()
If ($iChannelNum < 0) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; The Sophos router will send this initial prompt:

; 	Main Menu
; 
; 	    1.  Network  Configuration
; 	    2.  System   Configuration
; 	    3.  Route    Configuration
; 	    4.  Device Console
; 	    5.  Device Management
; 	    6.  VPN Management
; 	    7.  Shutdown/Reboot Device
; 	    0.  Exit
; 
; 	    Select Menu Number [0-7]: 

$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"Select Menu Number","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Show what we received so far:
ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

; Send "4" <RETURN> to mimic typing in a shell session.
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"4" & @LF,"utf-8")
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; We should get a "console>" prompt.
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"console>","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

; Send a command and get the output..
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"dnslookup host google.com" & @LF,"utf-8")
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"console>","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

; Send another command and get the output, and so on...
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"dnslookup host microsoft.com" & @LF,"utf-8")
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"console>","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

$oSsh.Disconnect