DataFlex
DataFlex
SSH Commands to a Sophos Router Device Console
See more SSH Examples
Demonstrates establishing an SSH session with a Sophos XG router and sending commands in its device console session. The router presents a numbered main menu; the example selects the Device Console entry and then runs commands at the resulting console> prompt.
Note: QuickShell allocates a PTY, which is what a network device console expects. The device presents an interactive prompt, and each command's output is read up to the next prompt.
Background: Menu-driven appliances require the client to navigate the interface exactly as a human would — read the menu, send the keystroke that selects an entry, then wait for the next prompt. Because the prompt changes as you move between menu levels, the pattern you match on must change too, which is why this example matches the menu text first and
console> afterward.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSsh
Integer iPort
String sPassword
Integer iChannelNum
Boolean iCaseSensitive
String sCmdOutput
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates establishing an SSH session with a Sophos XG router and sending commands in its
// device console session.
//
// Note: QuickShell allocates a PTY, which is what a device console expects. Navigating the
// device's menus is a matter of reading up to each prompt and sending the expected keystrokes.
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iPort
Get ComConnect Of hoSsh "172.16.16.16" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComQuickShell Of hoSsh To iChannelNum
If (iChannelNum < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// IMPORTANT: Set a read timeout before receiving until a match. ReadTimeoutMs defaults to 0,
// which means no limit -- without it, this call waits forever if the received data never
// contains a match.
Set ComReadTimeoutMs Of hoSsh To 15000
Move True To iCaseSensitive
// The router presents a numbered main menu ending with "Select Menu Number [0-7]: ".
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum "Select Menu Number" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sCmdOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sCmdOutput
// Choose menu item 4 (Device Console), exactly as if typing it in a terminal.
Get ComChannelSendString Of hoSsh iChannelNum "4" + (character(10)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// The device console prompt is "console>".
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum "console>" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sCmdOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sCmdOutput
// Send a console command and read its output up to the next prompt.
Get ComChannelSendString Of hoSsh iChannelNum "dnslookup host google.com" + (character(10)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum "console>" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sCmdOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sCmdOutput
// Additional commands follow the same pattern.
Get ComChannelSendString Of hoSsh iChannelNum "dnslookup host microsoft.com" + (character(10)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum "console>" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sCmdOutput
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sCmdOutput
Send ComDisconnect To hoSsh
End_Procedure