Sample code for 30+ languages & platforms
Objective-C

Open a Forwarded Channel through an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshNewChannel, which opens a port-forwarded channel through an authenticated SSH tunnel to a destination host and port, populating a Socket with the connected channel.

Background. Set the ssl argument to true to establish TLS to the destination inside the SSH tunnel. The returned channel socket is then used to send and receive with the destination.

Chilkat Objective-C Downloads

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

BOOL success = NO;

CkoSocket *socket = [[CkoSocket alloc] init];

//  Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
//  not required.
success = [socket SshOpenTunnel: @"ssh.example.com" sshPort: [NSNumber numberWithInt: 22]];
if (success == NO) {
    NSLog(@"%@",socket.LastErrorText);
    return;
}

//  Authenticate the SSH session before opening forwarded channels.
//  The SSH password should come from a secure source rather than being hard-coded.
NSString *sshPassword = @"mySshPassword";
success = [socket SshAuthenticatePw: @"sshUser" sshPassword: sshPassword];
if (success == NO) {
    NSLog(@"%@",socket.LastErrorText);
    return;
}

//  Open a port-forwarded channel through the authenticated SSH tunnel to the destination host and
//  port, populating a Socket with the connected channel.  Set the 3rd argument to true to establish
//  TLS to the destination inside the tunnel.
BOOL bTls = NO;
int maxWaitMs = 20000;
CkoSocket *channel = [[CkoSocket alloc] init];
success = [socket SshNewChannel: @"db.internal" port: [NSNumber numberWithInt: 5432] ssl: bTls maxWaitMs: [NSNumber numberWithInt: maxWaitMs] channel: channel];
if (success == NO) {
    NSLog(@"%@",socket.LastErrorText);
    return;
}

//  Use the channel socket to send and receive with the destination through the tunnel.
NSLog(@"%@",@"Forwarded channel opened to the destination.");