PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkSsh.pb"
IncludeFile "CkStringArray.pb"
Procedure ChilkatExample()
success.i = 0
; 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.
ssh.i = CkSsh::ckCreate()
If ssh.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
port.i = 22
success = CkSsh::ckConnect(ssh,"172.16.16.100",port)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
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.
password.s = "mySshPassword"
success = CkSsh::ckAuthenticatePw(ssh,"mySshLogin",password)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
channelNum.i = CkSsh::ckQuickShell(ssh)
If channelNum < 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
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.
CkSsh::setCkReadTimeoutMs(ssh, 15000)
caseSensitive.i = 1
; In unprivileged mode the switch prompt ends with ">".
success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,">","utf-8",caseSensitive)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
received.s = CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")
If CkSsh::ckLastMethodSuccess(ssh) = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
Debug received
; Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
success = CkSsh::ckChannelSendString(ssh,channelNum,"ena" + Chr(13),"utf-8")
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,"Password:","utf-8",caseSensitive)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
received = CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")
If CkSsh::ckLastMethodSuccess(ssh) = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
Debug received
; The enable password is separate from the login password, and likewise should come from a
; secure source rather than being hard-coded.
enablePassword.s = "myEnablePassword"
success = CkSsh::ckChannelSendString(ssh,channelNum,enablePassword,"utf-8")
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
success = CkSsh::ckChannelSendString(ssh,channelNum,Chr(13),"utf-8")
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
; In privileged mode the prompt ends with "#" instead of ">".
success = CkSsh::ckChannelReceiveUntilMatch(ssh,channelNum,"#","utf-8",caseSensitive)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
received = CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")
If CkSsh::ckLastMethodSuccess(ssh) = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
EndIf
Debug received
; Run a command whose output is delivered a page at a time.
success = CkSsh::ckChannelSendString(ssh,channelNum,"show running-config" + Chr(13),"utf-8")
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
ProcedureReturn
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.
saMatch.i = CkStringArray::ckCreate()
If saMatch.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringArray::ckAppend(saMatch,"MySwitchName#")
CkStringArray::ckAppend(saMatch,"--More--")
sbReceived.i = CkStringBuilder::ckCreate()
If sbReceived.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
moreComing.i = 1
While moreComing
success = CkSsh::ckChannelReceiveUntilMatchN(ssh,channelNum,saMatch,"utf-8",caseSensitive)
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
CkStringArray::ckDispose(saMatch)
CkStringBuilder::ckDispose(sbReceived)
ProcedureReturn
EndIf
CkStringBuilder::ckClear(sbReceived)
pageText.s = CkSsh::ckGetReceivedText(ssh,channelNum,"utf-8")
If CkSsh::ckLastMethodSuccess(ssh) = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
CkStringArray::ckDispose(saMatch)
CkStringBuilder::ckDispose(sbReceived)
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbReceived,pageText)
Debug pageText
; If this page ended with "--More--", send a SPACE to request the next page.
moreComing = CkStringBuilder::ckContains(sbReceived,"--More--",caseSensitive)
If moreComing
success = CkSsh::ckChannelSendString(ssh,channelNum," ","utf-8")
If success = 0
Debug CkSsh::ckLastErrorText(ssh)
CkSsh::ckDispose(ssh)
CkStringArray::ckDispose(saMatch)
CkStringBuilder::ckDispose(sbReceived)
ProcedureReturn
EndIf
EndIf
Wend
CkSsh::ckDisconnect(ssh)
CkSsh::ckDispose(ssh)
CkStringArray::ckDispose(saMatch)
CkStringBuilder::ckDispose(sbReceived)
ProcedureReturn
EndProcedure