Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Ssh
integer li_Port
string ls_Password
integer li_ChannelNum
integer li_CaseSensitive
string ls_CmdOutput

li_Success = 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.

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
    destroy loo_Ssh
    MessageBox("Error","Connecting to COM object failed")
    return
end if

li_Port = 22
li_Success = loo_Ssh.Connect("172.16.16.16",li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Ssh.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_ChannelNum = loo_Ssh.QuickShell()
if li_ChannelNum < 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  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.
loo_Ssh.ReadTimeoutMs = 15000

li_CaseSensitive = 1

//  The router presents a numbered main menu ending with "Select Menu Number [0-7]: ".
li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"Select Menu Number","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug ls_CmdOutput

//  Choose menu item 4 (Device Console), exactly as if typing it in a terminal.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"4~n","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  The device console prompt is "console>".
li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"console>","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug ls_CmdOutput

//  Send a console command and read its output up to the next prompt.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"dnslookup host google.com~n","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"console>","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug ls_CmdOutput

//  Additional commands follow the same pattern.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"dnslookup host microsoft.com~n","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"console>","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_CmdOutput = loo_Ssh.GetReceivedText(li_ChannelNum,"utf-8")
if loo_Ssh.LastMethodSuccess = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

Write-Debug ls_CmdOutput

loo_Ssh.Disconnect()


destroy loo_Ssh