Sample code for 30+ languages & platforms
PHP

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 PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Socket;
use Chilkat2\Ssh;

$ssh = new Ssh();

// Connect and authenticate a standalone SSH object.
try {
    $ssh->Connect('ssh.example.com', 22);

    // The SSH password should come from a secure source rather than being hard-coded.
    $sshPassword = 'mySshPassword';
    $ssh->AuthenticatePw('sshUser', $sshPassword);

    $socket = new Socket();

    // 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.
    $socket->UseSsh($ssh);

    // Connect to the destination through the SSH tunnel.
    $bTls = false;
    $maxWaitMs = 5000;
    $socket->Connect('db.internal', 5432, $bTls, $maxWaitMs);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Connected to the destination through the SSH tunnel.\n";