Objective-C
Objective-C
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 Objective-C Downloads
#import <CkoSsh.h>
#import <NSString.h>
#import <CkoStringArray.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// 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.
CkoSsh *ssh = [[CkoSsh alloc] init];
int port = 22;
success = [ssh Connect: @"172.16.16.100" port: [NSNumber numberWithInt: port]];
if (success == NO) {
NSLog(@"%@",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.
NSString *password = @"mySshPassword";
success = [ssh AuthenticatePw: @"mySshLogin" password: password];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
int channelNum = [[ssh QuickShell] intValue];
if (channelNum < 0) {
NSLog(@"%@",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 = [NSNumber numberWithInt:15000];
BOOL caseSensitive = YES;
// In unprivileged mode the switch prompt ends with ">".
success = [ssh ChannelReceiveUntilMatch: [NSNumber numberWithInt: channelNum] matchPattern: @">" charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
NSString *received = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
NSLog(@"%@",received);
// Send "ena" to enter privileged mode. Cisco devices expect a bare CR to terminate a command.
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: @"ena\r" charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
success = [ssh ChannelReceiveUntilMatch: [NSNumber numberWithInt: channelNum] matchPattern: @"Password:" charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
received = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
NSLog(@"%@",received);
// The enable password is separate from the login password, and likewise should come from a
// secure source rather than being hard-coded.
NSString *enablePassword = @"myEnablePassword";
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: enablePassword charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: @"\r" charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
// In privileged mode the prompt ends with "#" instead of ">".
success = [ssh ChannelReceiveUntilMatch: [NSNumber numberWithInt: channelNum] matchPattern: @"#" charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
received = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
NSLog(@"%@",received);
// Run a command whose output is delivered a page at a time.
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: @"show running-config\r" charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",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.
CkoStringArray *saMatch = [[CkoStringArray alloc] init];
[saMatch Append: @"MySwitchName#"];
[saMatch Append: @"--More--"];
CkoStringBuilder *sbReceived = [[CkoStringBuilder alloc] init];
BOOL moreComing = YES;
while (moreComing) {
success = [ssh ChannelReceiveUntilMatchN: [NSNumber numberWithInt: channelNum] matchPatterns: saMatch charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
[sbReceived Clear];
NSString *pageText = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
[sbReceived Append: pageText];
NSLog(@"%@",pageText);
// If this page ended with "--More--", send a SPACE to request the next page.
moreComing = [sbReceived Contains: @"--More--" caseSensitive: caseSensitive];
if (moreComing) {
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: @" " charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
}
}
[ssh Disconnect];