Objective-C
Objective-C
Route Socket Connections through an SSH Object
See more Socket/SSL/TLS Examples
Demonstrates Socket.UseSsh, which configures a socket to route subsequent Connect calls through an already connected and authenticated Ssh object using SSH port forwarding.
Background. After UseSsh, the destination host and port passed to Connect are reached through the SSH tunnel rather than by a direct TCP connection. This is one of several SSH-tunneling approaches the Socket class supports.
Chilkat Objective-C Downloads
#import <CkoSsh.h>
#import <NSString.h>
#import <CkoSocket.h>
BOOL success = NO;
CkoSsh *ssh = [[CkoSsh alloc] init];
// Connect and authenticate a standalone SSH object.
success = [ssh Connect: @"ssh.example.com" port: [NSNumber numberWithInt: 22]];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
// The SSH password should come from a secure source rather than being hard-coded.
NSString *sshPassword = @"mySshPassword";
success = [ssh AuthenticatePw: @"sshUser" password: sshPassword];
if (success == NO) {
NSLog(@"%@",ssh.LastErrorText);
return;
}
CkoSocket *socket = [[CkoSocket alloc] init];
// Route subsequent Connect calls through the already connected and authenticated SSH object. The
// destination is reached by SSH port forwarding rather than by a direct TCP connection.
success = [socket UseSsh: ssh];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
// Connect to the destination through the SSH tunnel.
BOOL bTls = NO;
int maxWaitMs = 5000;
success = [socket Connect: @"db.internal" port: [NSNumber numberWithInt: 5432] ssl: bTls maxWaitMs: [NSNumber numberWithInt: maxWaitMs]];
if (success == NO) {
NSLog(@"%@",socket.LastErrorText);
return;
}
NSLog(@"%@",@"Connected to the destination through the SSH tunnel.");