Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// 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.
loSsh = createobject("CkSsh")
lnPort = 22
llSuccess = loSsh.Connect("172.16.16.100",lnPort)
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
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.
lcPassword = "mySshPassword"
llSuccess = loSsh.AuthenticatePw("mySshLogin",lcPassword)
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
lnChannelNum = loSsh.QuickShell()
if (lnChannelNum < 0) then
? loSsh.LastErrorText
release loSsh
return
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.
loSsh.ReadTimeoutMs = 15000
llCaseSensitive = .T.
// In unprivileged mode the switch prompt ends with ">".
llSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,">","utf-8",llCaseSensitive)
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
lcReceived = loSsh.GetReceivedText(lnChannelNum,"utf-8")
if (loSsh.LastMethodSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
? lcReceived
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
llSuccess = loSsh.ChannelSendString(lnChannelNum,"ena" + Chr(13),"utf-8")
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
llSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"Password:","utf-8",llCaseSensitive)
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
lcReceived = loSsh.GetReceivedText(lnChannelNum,"utf-8")
if (loSsh.LastMethodSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
? lcReceived
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
lcEnablePassword = "myEnablePassword"
llSuccess = loSsh.ChannelSendString(lnChannelNum,lcEnablePassword,"utf-8")
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
llSuccess = loSsh.ChannelSendString(lnChannelNum,Chr(13),"utf-8")
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
// In privileged mode the prompt ends with "#" instead of ">".
llSuccess = loSsh.ChannelReceiveUntilMatch(lnChannelNum,"#","utf-8",llCaseSensitive)
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
lcReceived = loSsh.GetReceivedText(lnChannelNum,"utf-8")
if (loSsh.LastMethodSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
endif
? lcReceived
// Run a command whose output is delivered a page at a time.
llSuccess = loSsh.ChannelSendString(lnChannelNum,"show running-config" + Chr(13),"utf-8")
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
return
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.
loSaMatch = createobject("CkStringArray")
loSaMatch.Append("MySwitchName#")
loSaMatch.Append("--More--")
loSbReceived = createobject("CkStringBuilder")
llMoreComing = .T.
do while llMoreComing
llSuccess = loSsh.ChannelReceiveUntilMatchN(lnChannelNum,loSaMatch,"utf-8",llCaseSensitive)
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
release loSaMatch
release loSbReceived
return
endif
loSbReceived.Clear()
lcPageText = loSsh.GetReceivedText(lnChannelNum,"utf-8")
if (loSsh.LastMethodSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
release loSaMatch
release loSbReceived
return
endif
loSbReceived.Append(lcPageText)
? lcPageText
// If this page ended with "--More--", send a SPACE to request the next page.
llMoreComing = loSbReceived.Contains("--More--",llCaseSensitive)
if (llMoreComing) then
llSuccess = loSsh.ChannelSendString(lnChannelNum," ","utf-8")
if (llSuccess = .F.) then
? loSsh.LastErrorText
release loSsh
release loSaMatch
release loSbReceived
return
endif
endif
enddo
loSsh.Disconnect()
release loSsh
release loSaMatch
release loSbReceived