Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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.
let ssh = CkoSsh()!
var port: Int = 22
success = ssh.connect(hostname: "172.16.16.100", port: port)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
// 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.
var password: String? = "mySshPassword"
success = ssh.authenticatePw(login: "mySshLogin", password: password)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
var channelNum: Int = ssh.quickShell().intValue
if channelNum < 0 {
print("\(ssh.lastErrorText!)")
return
}
// 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.
ssh.readTimeoutMs = 15000
var caseSensitive: Bool = true
// In unprivileged mode the switch prompt ends with ">".
success = ssh.channelReceive(untilMatch: channelNum, matchPattern: ">", charset: "utf-8", caseSensitive: caseSensitive)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
var received: String? = ssh.getReceivedText(channelNum: channelNum, charset: "utf-8")
if ssh.lastMethodSuccess == false {
print("\(ssh.lastErrorText!)")
return
}
print("\(received!)")
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
success = ssh.channelSendString(channelNum: channelNum, strData: "ena\r", charset: "utf-8")
if success == false {
print("\(ssh.lastErrorText!)")
return
}
success = ssh.channelReceive(untilMatch: channelNum, matchPattern: "Password:", charset: "utf-8", caseSensitive: caseSensitive)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
received = ssh.getReceivedText(channelNum: channelNum, charset: "utf-8")
if ssh.lastMethodSuccess == false {
print("\(ssh.lastErrorText!)")
return
}
print("\(received!)")
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
var enablePassword: String? = "myEnablePassword"
success = ssh.channelSendString(channelNum: channelNum, strData: enablePassword, charset: "utf-8")
if success == false {
print("\(ssh.lastErrorText!)")
return
}
success = ssh.channelSendString(channelNum: channelNum, strData: "\r", charset: "utf-8")
if success == false {
print("\(ssh.lastErrorText!)")
return
}
// In privileged mode the prompt ends with "#" instead of ">".
success = ssh.channelReceive(untilMatch: channelNum, matchPattern: "#", charset: "utf-8", caseSensitive: caseSensitive)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
received = ssh.getReceivedText(channelNum: channelNum, charset: "utf-8")
if ssh.lastMethodSuccess == false {
print("\(ssh.lastErrorText!)")
return
}
print("\(received!)")
// Run a command whose output is delivered a page at a time.
success = ssh.channelSendString(channelNum: channelNum, strData: "show running-config\r", charset: "utf-8")
if success == false {
print("\(ssh.lastErrorText!)")
return
}
// 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.
let saMatch = CkoStringArray()!
saMatch.append(str: "MySwitchName#")
saMatch.append(str: "--More--")
let sbReceived = CkoStringBuilder()!
var moreComing: Bool = true
while moreComing {
success = ssh.channelReceive(untilMatchN: channelNum, matchPatterns: saMatch, charset: "utf-8", caseSensitive: caseSensitive)
if success == false {
print("\(ssh.lastErrorText!)")
return
}
sbReceived.clear()
var pageText: String? = ssh.getReceivedText(channelNum: channelNum, charset: "utf-8")
if ssh.lastMethodSuccess == false {
print("\(ssh.lastErrorText!)")
return
}
sbReceived.append(value: pageText)
print("\(pageText!)")
// If this page ended with "--More--", send a SPACE to request the next page.
moreComing = sbReceived.contains(str: "--More--", caseSensitive: caseSensitive)
if moreComing {
success = ssh.channelSendString(channelNum: channelNum, strData: " ", charset: "utf-8")
if success == false {
print("\(ssh.lastErrorText!)")
return
}
}
}
ssh.disconnect()
}