Sample code for 30+ languages & platforms
PowerBuilder

SSH to a Cisco Switch - Processing "More" Responses

See more SSH Examples

Demonstrates connecting to a Cisco switch, entering privileged mode with the ena command, and running a command whose response is paged. Each page ends with --More-- and requires a SPACE character to request the next page.

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: Paged output is the console equivalent of a pager like more, and a client must reproduce the keypress a human would make. Matching a set of patterns is what makes this tractable: each read ends either at the device prompt (output complete) or at --More-- (another page waiting), and the code branches accordingly. Note the prompt pattern includes the device name rather than a bare #, because that character also appears inside configuration text and would otherwise match far too early.

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_Received
string ls_EnablePassword
oleobject loo_SaMatch
oleobject loo_SbReceived
integer li_MoreComing
string ls_PageText

li_Success = 0

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

//  Demonstrates connecting to a Cisco switch, entering privileged mode, and running a command
//  whose response is paged -- each page ends with "--More--" and requires a SPACE character to
//  request the next page.

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.100",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

//  In unprivileged mode the switch prompt ends with ">".
li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,">","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_Received = 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_Received

//  Send "ena" to enter privileged mode.  Cisco devices expect a bare CR to terminate a command.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"ena~r","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,"Password:","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_Received = 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_Received

//  The enable password is separate from the login password, and likewise should come from a
//  secure source rather than being hard-coded.
ls_EnablePassword = "myEnablePassword"
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,ls_EnablePassword,"utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"~r","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  In privileged mode the prompt ends with "#" instead of ">".
li_Success = loo_Ssh.ChannelReceiveUntilMatch(li_ChannelNum,"#","utf-8",li_CaseSensitive)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

ls_Received = 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_Received

//  Run a command whose output is delivered a page at a time.
li_Success = loo_Ssh.ChannelSendString(li_ChannelNum,"show running-config~r","utf-8")
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Ssh
    return
end if

//  Each read ends either at the device prompt (output complete) or at "--More--" (another page
//  is waiting).  Match the full prompt rather than a bare "#", because "#" also appears inside
//  configuration text and would match too early.
loo_SaMatch = create oleobject
li_rc = loo_SaMatch.ConnectToNewObject("Chilkat.StringArray")

loo_SaMatch.Append("MySwitchName#")
loo_SaMatch.Append("--More--")

loo_SbReceived = create oleobject
li_rc = loo_SbReceived.ConnectToNewObject("Chilkat.StringBuilder")

li_MoreComing = 1

do while li_MoreComing
    li_Success = loo_Ssh.ChannelReceiveUntilMatchN(li_ChannelNum,loo_SaMatch,"utf-8",li_CaseSensitive)
    if li_Success = 0 then
        Write-Debug loo_Ssh.LastErrorText
        destroy loo_Ssh
        destroy loo_SaMatch
        destroy loo_SbReceived
        return
    end if

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

    loo_SbReceived.Append(ls_PageText)
    Write-Debug ls_PageText

    //  If this page ended with "--More--", send a SPACE to request the next page.
    li_MoreComing = loo_SbReceived.Contains("--More--",li_CaseSensitive)
    if li_MoreComing then
        li_Success = loo_Ssh.ChannelSendString(li_ChannelNum," ","utf-8")
        if li_Success = 0 then
            Write-Debug loo_Ssh.LastErrorText
            destroy loo_Ssh
            destroy loo_SaMatch
            destroy loo_SbReceived
            return
        end if

    end if

loop

loo_Ssh.Disconnect()


destroy loo_Ssh
destroy loo_SaMatch
destroy loo_SbReceived