DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSsh
Integer iPort
String sPassword
Integer iChannelNum
Boolean iCaseSensitive
String sReceived
String sEnablePassword
Variant vSaMatch
Handle hoSaMatch
Handle hoSbReceived
Boolean iMoreComing
String sPageText
String sTemp1
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSsh)) To hoSsh
If (Not(IsComObjectCreated(hoSsh))) Begin
Send CreateComObject of hoSsh
End
Move 22 To iPort
Get ComConnect Of hoSsh "172.16.16.100" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSsh "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComQuickShell Of hoSsh To iChannelNum
If (iChannelNum < 0) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Set ComReadTimeoutMs Of hoSsh To 15000
Move True To iCaseSensitive
// In unprivileged mode the switch prompt ends with ">".
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum ">" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sReceived
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sReceived
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
Get ComChannelSendString Of hoSsh iChannelNum "ena" + (character(13)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum "Password:" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sReceived
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sReceived
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
Move "myEnablePassword" To sEnablePassword
Get ComChannelSendString Of hoSsh iChannelNum sEnablePassword "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComChannelSendString Of hoSsh iChannelNum (character(13)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// In privileged mode the prompt ends with "#" instead of ">".
Get ComChannelReceiveUntilMatch Of hoSsh iChannelNum "#" "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sReceived
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Showln sReceived
// Run a command whose output is delivered a page at a time.
Get ComChannelSendString Of hoSsh iChannelNum "show running-config" + (character(13)) "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get Create (RefClass(cComCkStringArray)) To hoSaMatch
If (Not(IsComObjectCreated(hoSaMatch))) Begin
Send CreateComObject of hoSaMatch
End
Get ComAppend Of hoSaMatch "MySwitchName#" To iSuccess
Get ComAppend Of hoSaMatch "--More--" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbReceived
If (Not(IsComObjectCreated(hoSbReceived))) Begin
Send CreateComObject of hoSbReceived
End
Move True To iMoreComing
While iMoreComing
Get pvComObject of hoSaMatch to vSaMatch
Get ComChannelReceiveUntilMatchN Of hoSsh iChannelNum vSaMatch "utf-8" iCaseSensitive To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Send ComClear To hoSbReceived
Get ComGetReceivedText Of hoSsh iChannelNum "utf-8" To sPageText
Get ComLastMethodSuccess Of hoSsh To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAppend Of hoSbReceived sPageText To iSuccess
Showln sPageText
// If this page ended with "--More--", send a SPACE to request the next page.
Get ComContains Of hoSbReceived "--More--" iCaseSensitive To iMoreComing
If (iMoreComing) Begin
Get ComChannelSendString Of hoSsh iChannelNum " " "utf-8" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSsh To sTemp1
Showln sTemp1
Procedure_Return
End
End
Loop
Send ComDisconnect To hoSsh
End_Procedure