Sample code for 30+ languages & platforms
Objective-C

SSH ChannelReceiveUntilMatchN (Match Any of Several Prompts)

See more SSH Examples

Demonstrates ChannelReceiveUntilMatchN, which receives until any one of several patterns is matched. This is useful when driving an interactive session where the shell prompt differs from device to device.

Note: This example deliberately requests a PTY. Matching a shell prompt only works in an interactive session — without a PTY the shell runs non-interactively and never prints a prompt at all. For scripted use, omit the PTY and match on a marker your own command echoes.

Background: Real sessions have more than one possible next output, so waiting for one specific string risks hanging when something else appears. Matching a set lets the code proceed on whichever actually occurs — a user prompt, a root prompt, or a device-specific one. A "dumb" terminal type is requested so the remote side does not mix ANSI escape sequences into the output, which would otherwise complicate matching. Remember that retrieving text clears the receive buffer; leaving a prompt buffered would make the next receive match immediately and return the wrong output.

Chilkat Objective-C Downloads

Objective-C
#import <CkoSsh.h>
#import <CkoStringArray.h>
#import <NSString.h>

BOOL success = NO;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Demonstrates ChannelReceiveUntilMatchN, which receives until ANY one of several patterns is
//  matched.  This is useful when driving an interactive session where the shell prompt varies
//  from device to device.
//  
//  Note: This example deliberately DOES request a PTY.  Matching a shell prompt only works in an
//  interactive session -- without a PTY the shell runs non-interactively and never prints a
//  prompt at all.  For scripted use where no prompt is needed, omit the PTY and match on a
//  marker your own command echoes instead.

CkoSsh *ssh = [[CkoSsh alloc] init];

//  The remote shell prompt may be any of these, depending on the device.
CkoStringArray *saPrompts = [[CkoStringArray alloc] init];
[saPrompts Append: @"~$"];
[saPrompts Append: @"mars#"];
[saPrompts Append: @"jupiter%"];
[saPrompts Append: @"chilkat$"];
[saPrompts Append: @"admin>"];

NSString *hostname = @"ssh.example.com";
int port = 22;
success = [ssh Connect: hostname 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 OpenSessionChannel] intValue];
if (channelNum < 0) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

//  Request a pseudo-terminal so the shell runs interactively and prints prompts.  A "dumb"
//  terminal is used so that ANSI escape sequences are not mixed into the output.
NSString *termType = @"dumb";
int widthInChars = 120;
int heightInChars = 40;
int pixWidth = 0;
int pixHeight = 0;
success = [ssh SendReqPty: [NSNumber numberWithInt: channelNum] xTermEnvVar: termType widthInChars: [NSNumber numberWithInt: widthInChars] heightInRows: [NSNumber numberWithInt: heightInChars] pixWidth: [NSNumber numberWithInt: pixWidth] pixHeight: [NSNumber numberWithInt: pixHeight]];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

success = [ssh SendReqShell: [NSNumber numberWithInt: channelNum]];
if (success == NO) {
    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;

//  Read the initial login text up to the first prompt.
success = [ssh ChannelReceiveUntilMatchN: [NSNumber numberWithInt: channelNum] matchPatterns: saPrompts charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSString *cmdOutput = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSLog(@"%@",cmdOutput);

//  ---- 1st command ----
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: @"cd /tmp\n" charset: @"utf-8"];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

success = [ssh ChannelReceiveUntilMatchN: [NSNumber numberWithInt: channelNum] matchPatterns: saPrompts charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

//  Retrieving the text also clears the receive buffer, which matters: if a prompt were left
//  buffered, the next receive would match it immediately and return the wrong output.
cmdOutput = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSLog(@"%@",cmdOutput);

//  ---- 2nd command ----
success = [ssh ChannelSendString: [NSNumber numberWithInt: channelNum] strData: @"ls\n" charset: @"utf-8"];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

success = [ssh ChannelReceiveUntilMatchN: [NSNumber numberWithInt: channelNum] matchPatterns: saPrompts charset: @"utf-8" caseSensitive: caseSensitive];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

cmdOutput = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSLog(@"%@",cmdOutput);

//  Finish the session.
success = [ssh ChannelSendEof: [NSNumber numberWithInt: channelNum]];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

success = [ssh ChannelReceiveToClose: [NSNumber numberWithInt: channelNum]];
if (success == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

cmdOutput = [ssh GetReceivedText: [NSNumber numberWithInt: channelNum] charset: @"utf-8"];
if (ssh.LastMethodSuccess == NO) {
    NSLog(@"%@",ssh.LastErrorText);
    return;
}

NSLog(@"%@",cmdOutput);

[ssh Disconnect];