Sample code for 30+ languages & platforms
Objective-C

Get the Type of an Active SSH Channel

See more SSH Examples

Demonstrates the Chilkat Ssh.GetChannelType method, which returns the type of the channel at a zero-based enumeration index. The only argument is the index — an enumeration position, not a channel number. Values include session, x11, forwarded-tcpip, and direct-tcpip.

Background: Paired with GetChannelNumber, this turns the channel collection into a readable inventory of what a connection is currently doing — which channels are running commands (session) versus tunneling TCP traffic (direct-tcpip). That is valuable for diagnostics and for application code that manages several channel kinds at once. An out-of-range index causes the string-returning method to report failure.

Chilkat Objective-C Downloads

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

BOOL success = NO;

//  Demonstrates the Ssh.GetChannelType method, which returns the type of the channel at a
//  zero-based enumeration index.  The only argument is the index, which is an enumeration
//  position and not a channel number.  Values include "session" and "direct-tcpip".

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

int sshPort = 22;
success = [ssh Connect: @"ssh.example.com" port: [NSNumber numberWithInt: sshPort]];
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;
}

//  Enumerate the active channels and report each one's number and type.
int n = [ssh.NumOpenChannels intValue];
int i;
for (i = 0; i <= n - 1; i++) {
    int chNum = [[ssh GetChannelNumber: [NSNumber numberWithInt: i]] intValue];
    NSString *chType = [ssh GetChannelType: [NSNumber numberWithInt: i]];
    if (ssh.LastMethodSuccess == NO) {
        NSLog(@"%@",ssh.LastErrorText);
        return;
    }

    NSLog(@"%@%d%@%@",@"Channel ",chNum,@" is of type ",chType);
}

[ssh Disconnect];