Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSsh
LOCAL lnPort
LOCAL lcPassword
LOCAL lnChannelNum
LOCAL lnCaseSensitive
LOCAL lcCmdOutput

lnSuccess = 0

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

loSsh = CreateObject('Chilkat.Ssh')

lnPort = 22
lnSuccess = loSsh.Connect("172.16.16.16",lnPort)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  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.
lcPassword = "mySshPassword"

lnSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lnChannelNum = loSsh.QuickShell()
IF (lnChannelNum < 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  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.
loSsh.ReadTimeoutMs = 15000

lnCaseSensitive = 1

*  The router presents a numbered main menu ending with "Select Menu Number [0-7]: ".
lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"Select Menu Number","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

? lcCmdOutput

*  Choose menu item 4 (Device Console), exactly as if typing it in a terminal.
lnSuccess = loSsh.ChannelSendString(lnChannelNum,"4" + CHR(10),"utf-8")
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

*  The device console prompt is "console>".
lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"console>","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

? lcCmdOutput

*  Send a console command and read its output up to the next prompt.
lnSuccess = loSsh.ChannelSendString(lnChannelNum,"dnslookup host google.com" + CHR(10),"utf-8")
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"console>","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

? lcCmdOutput

*  Additional commands follow the same pattern.
lnSuccess = loSsh.ChannelSendString(lnChannelNum,"dnslookup host microsoft.com" + CHR(10),"utf-8")
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lnSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"console>","utf-8",lnCaseSensitive)
IF (lnSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

lcCmdOutput = loSsh.GetReceivedText(lnChannelNum,"utf-8")
IF (loSsh.LastMethodSuccess = 0) THEN
    ? loSsh.LastErrorText
    RELEASE loSsh
    CANCEL
ENDIF

? lcCmdOutput

loSsh.Disconnect()

RELEASE loSsh