Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(AutoIt) SSH to Cisco Switch - Processing "More" ResponsesDemonstrates 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--".
; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. $oSsh = ObjCreate("Chilkat.Ssh") Local $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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.