Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 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.
set ssh [new_CkSsh]
set port 22
set success [CkSsh_Connect $ssh "172.16.16.100" $port]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
# 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.
set password "mySshPassword"
set success [CkSsh_AuthenticatePw $ssh "mySshLogin" $password]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
set channelNum [CkSsh_QuickShell $ssh]
if {$channelNum < 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
# 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_put_ReadTimeoutMs $ssh 15000
set caseSensitive 1
# In unprivileged mode the switch prompt ends with ">".
set success [CkSsh_ChannelReceiveUntilMatch $ssh $channelNum ">" "utf-8" $caseSensitive]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
set received [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
puts "$received"
# Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
set success [CkSsh_ChannelSendString $ssh $channelNum "ena\r" "utf-8"]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
set success [CkSsh_ChannelReceiveUntilMatch $ssh $channelNum "Password:" "utf-8" $caseSensitive]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
set received [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
puts "$received"
# The enable password is separate from the login password, and likewise should come from a
# secure source rather than being hard-coded.
set enablePassword "myEnablePassword"
set success [CkSsh_ChannelSendString $ssh $channelNum $enablePassword "utf-8"]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
set success [CkSsh_ChannelSendString $ssh $channelNum "\r" "utf-8"]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
# In privileged mode the prompt ends with "#" instead of ">".
set success [CkSsh_ChannelReceiveUntilMatch $ssh $channelNum "#" "utf-8" $caseSensitive]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
set received [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
puts "$received"
# Run a command whose output is delivered a page at a time.
set success [CkSsh_ChannelSendString $ssh $channelNum "show running-config\r" "utf-8"]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
exit
}
# 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.
set saMatch [new_CkStringArray]
CkStringArray_Append $saMatch "MySwitchName#"
CkStringArray_Append $saMatch "--More--"
set sbReceived [new_CkStringBuilder]
set moreComing 1
while {$moreComing} {
set success [CkSsh_ChannelReceiveUntilMatchN $ssh $channelNum $saMatch "utf-8" $caseSensitive]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
delete_CkStringArray $saMatch
delete_CkStringBuilder $sbReceived
exit
}
CkStringBuilder_Clear $sbReceived
set pageText [CkSsh_getReceivedText $ssh $channelNum "utf-8"]
if {[CkSsh_get_LastMethodSuccess $ssh] == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
delete_CkStringArray $saMatch
delete_CkStringBuilder $sbReceived
exit
}
CkStringBuilder_Append $sbReceived $pageText
puts "$pageText"
# If this page ended with "--More--", send a SPACE to request the next page.
set moreComing [CkStringBuilder_Contains $sbReceived "--More--" $caseSensitive]
if {$moreComing} then {
set success [CkSsh_ChannelSendString $ssh $channelNum " " "utf-8"]
if {$success == 0} then {
puts [CkSsh_lastErrorText $ssh]
delete_CkSsh $ssh
delete_CkStringArray $saMatch
delete_CkStringBuilder $sbReceived
exit
}
}
}
CkSsh_Disconnect $ssh
delete_CkSsh $ssh
delete_CkStringArray $saMatch
delete_CkStringBuilder $sbReceived