Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

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

$oSsh = ObjCreate("Chilkat.Ssh")

$bSuccess = $oSsh.Connect("SSH_SERVER_DOMAIN_OR_IP_ADDRESS",22)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Authenticate using login/password:
$bSuccess = $oSsh.AuthenticatePw("myLogin","myPassword")
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Start a shell session.
Local $iChannelNum = $oSsh.QuickShell()
If ($iChannelNum < 0) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
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..)
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,">","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Show what we received so far:
ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

; Send the "ena" command to enable privileged mode.
; (For the Cisco switch, terminate command with a single CR char.)
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"ena" & @CR,"utf-8")
; Assume success for this example to make it shorter..

; Read to the "Password:" prompt.
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"Password:","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Show what we received...
ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

; Send the password.
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"MY_PASSWORD_FOR_ELEVATED_PRIVILEGE" & @CR,"utf-8")

; The prompt now changes from "Something>" to "Something#>
; Read until the new prompt..
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"#","utf-8",True)
If ($bSuccess <> True) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

; Show what we received...
ConsoleWrite($oSsh.GetReceivedText($iChannelNum,"utf-8") & @CRLF)

; 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.
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"show running-config" & @CR,"utf-8")

; Consume the response until we end with another prompt.
$oSaMatch = ObjCreate("Chilkat.StringArray")
; 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...
$oSaMatch.Append("YOUR_PROMPT#")
$oSaMatch.Append("--More--")

$oSbReceived = ObjCreate("Chilkat.StringBuilder")
Local $bMoreComing = True
While ($bMoreComing = True)

    $bSuccess = $oSsh.ChannelReceiveUntilMatchN($iChannelNum,$oSaMatch,"utf-8",True)
    If ($bSuccess <> True) Then
        ConsoleWrite($oSsh.LastErrorText & @CRLF)
        Exit
    EndIf

    $bMoreComing = False

    $oSbReceived.Clear 
    $oSbReceived.Append($oSsh.GetReceivedText($iChannelNum,"utf-8"))

    ConsoleWrite($oSbReceived.GetAsString() & @CRLF)

    If ($oSbReceived.Contains("--More--",True) = True) Then
        $bMoreComing = True

        ; Send a SPACE char just as if we were interactively pressing the SPACE key to get more output.
        $bSuccess = $oSsh.ChannelSendString($iChannelNum," ","utf-8")
    EndIf

Wend

$oSsh.Disconnect