Sample code for 30+ languages & platforms
PureBasic

SSH to Cisco Switch - Processing "More" Responses

See more SSH Examples

Demonstrates connecting to a Cisco switch, running a command to enable privileged mode, then running a command to get a paged response requiring the SPACE char to be sent to process "--More--".

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkSsh.pb"
IncludeFile "CkStringArray.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ssh.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSsh::ckConnect(ssh,"SSH_SERVER_DOMAIN_OR_IP_ADDRESS",22)
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ; Authenticate using login/password:
    success = CkSsh::ckAuthenticatePw(ssh,"myLogin","myPassword")
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ; Start a shell session.
    channelNum.i = CkSsh::ckQuickShell(ssh)
    If channelNum < 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ; If the CISCO switch returns a prompt with ">", then read until we get the prompt.
    ; (It's not actually required that we do this, but it helps to know that all is OK at this point..)
    success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,">","utf-8",1)
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ; Show what we received so far:
    Debug CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")

    ; Send the "ena" command to enable privileged mode.
    ; (For the Cisco switch, terminate command with a single CR char.)
    success = CkSsh::ckChannelSendString(ssh,channelNum,"ena" + Chr(13),"utf-8")
    ; Assume success for this example to make it shorter..

    ; Read to the "Password:" prompt.
    success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,"Password:","utf-8",1)
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ; Show what we received...
    Debug CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")

    ; Send the password.
    success = CkSsh::ckChannelSendString(ssh,channelNum,"MY_PASSWORD_FOR_ELEVATED_PRIVILEGE" + Chr(13),"utf-8")

    ; The prompt now changes from "Something>" to "Something#>
    ; Read until the new prompt..
    success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,"#","utf-8",1)
    If success <> 1
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    ; Show what we received...
    Debug CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")

    ; Send the "show running-config" command.
    ; The response will be in multiple pages, each ending with "--More--" and requiring a SPACE bar to be sent
    ; to get the next page.
    success = CkSsh::ckChannelSendString(ssh,channelNum,"show running-config" + Chr(13),"utf-8")

    ; Consume the response until we end with another prompt.
    saMatch.i = CkStringArray::ckCreate()
    If saMatch.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Change "YOUR_PROMPT" to your actual prompt.  We don't want to check for only "#" because
    ; it's not specific enough.  The data in the response could contain the "#" char...
    CkStringArray::ckAppend(saMatch,"YOUR_PROMPT#")
    CkStringArray::ckAppend(saMatch,"--More--")

    sbReceived.i = CkStringBuilder::ckCreate()
    If sbReceived.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    moreComing.i = 1
    While (moreComing = 1)

        success = CkSsh::ckChannelReceiveUntilMatchN(ssh,channelNum,saMatch,"utf-8",1)
        If success <> 1
            Debug CkSsh::ckLastErrorText(ssh)
            CkSsh::ckDispose(ssh)
            CkStringArray::ckDispose(saMatch)
            CkStringBuilder::ckDispose(sbReceived)
            ProcedureReturn
        EndIf

        moreComing = 0

        CkStringBuilder::ckClear(sbReceived)
        CkStringBuilder::ckAppend(sbReceived,CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8"))

        Debug CkStringBuilder::ckGetAsString(sbReceived)

        If CkStringBuilder::ckContains(sbReceived,"--More--",1) = 1
            moreComing = 1

            ; Send a SPACE char just as if we were interactively pressing the SPACE key to get more output.
            success = CkSsh::ckChannelSendString(ssh,channelNum," ","utf-8")
        EndIf

    Wend

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)
    CkStringArray::ckDispose(saMatch)
    CkStringBuilder::ckDispose(sbReceived)


    ProcedureReturn
EndProcedure