Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

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

$oSsh = ObjCreate("Chilkat.Ssh")

Local $iPort = 22
$bSuccess = $oSsh.Connect("172.16.16.100",$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
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.
Local $sPassword = "mySshPassword"

$bSuccess = $oSsh.AuthenticatePw("mySshLogin",$sPassword)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

Local $iChannelNum = $oSsh.QuickShell()
If ($iChannelNum < 0) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
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.
$oSsh.ReadTimeoutMs = 15000

Local $bCaseSensitive = True

;  In unprivileged mode the switch prompt ends with ">".
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,">","utf-8",$bCaseSensitive)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

Local $sReceived = $oSsh.GetReceivedText($iChannelNum,"utf-8")
If ($oSsh.LastMethodSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($sReceived & @CRLF)

;  Send "ena" to enter privileged mode.  Cisco devices expect a bare CR to terminate a command.
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"ena" & @CR,"utf-8")
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"Password:","utf-8",$bCaseSensitive)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

$sReceived = $oSsh.GetReceivedText($iChannelNum,"utf-8")
If ($oSsh.LastMethodSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($sReceived & @CRLF)

;  The enable password is separate from the login password, and likewise should come from a
;  secure source rather than being hard-coded.
Local $sEnablePassword = "myEnablePassword"
$bSuccess = $oSsh.ChannelSendString($iChannelNum,$sEnablePassword,"utf-8")
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oSsh.ChannelSendString($iChannelNum,@CR,"utf-8")
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

;  In privileged mode the prompt ends with "#" instead of ">".
$bSuccess = $oSsh.ChannelReceiveUntilMatch($iChannelNum,"#","utf-8",$bCaseSensitive)
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

$sReceived = $oSsh.GetReceivedText($iChannelNum,"utf-8")
If ($oSsh.LastMethodSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($sReceived & @CRLF)

;  Run a command whose output is delivered a page at a time.
$bSuccess = $oSsh.ChannelSendString($iChannelNum,"show running-config" & @CR,"utf-8")
If ($bSuccess = False) Then
    ConsoleWrite($oSsh.LastErrorText & @CRLF)
    Exit
EndIf

;  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.
$oSaMatch = ObjCreate("Chilkat.StringArray")
$oSaMatch.Append("MySwitchName#")
$oSaMatch.Append("--More--")

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

While $bMoreComing
    $bSuccess = $oSsh.ChannelReceiveUntilMatchN($iChannelNum,$oSaMatch,"utf-8",$bCaseSensitive)
    If ($bSuccess = False) Then
        ConsoleWrite($oSsh.LastErrorText & @CRLF)
        Exit
    EndIf

    $oSbReceived.Clear 
Local $sPageText = $oSsh.GetReceivedText($iChannelNum,"utf-8")
    If ($oSsh.LastMethodSuccess = False) Then
        ConsoleWrite($oSsh.LastErrorText & @CRLF)
        Exit
    EndIf

    $oSbReceived.Append($sPageText)
    ConsoleWrite($sPageText & @CRLF)

    ;  If this page ended with "--More--", send a SPACE to request the next page.
    $bMoreComing = $oSbReceived.Contains("--More--",$bCaseSensitive)
    If ($bMoreComing) Then
        $bSuccess = $oSsh.ChannelSendString($iChannelNum," ","utf-8")
        If ($bSuccess = False) Then
            ConsoleWrite($oSsh.LastErrorText & @CRLF)
            Exit
        EndIf

    EndIf

Wend

$oSsh.Disconnect