PHP code example of savvii / ssh-tunnel

1. Go to this page and download the library: Download savvii/ssh-tunnel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

savvii / ssh-tunnel example snippets


use Savvii\SshTunnel\SshTunnel;

$tunnel = new SshTunnel(
    sshUsername: 'myuser',
    sshHost: 'jumpserver.example.com',
    sshPort: 22,
    bindHost: 'remote-db-host.local.lan',
    bindPort: 3306    
);

$db = new PDO(
    sprintf(
        "mysql:host=%s;port=%d",
        $tunnel->localAddress,
        $tunnel->localPort
    )
);

function connect(): void
{
    $tunnel = new SshTunnel(...);
}

connect();
// At this point the SSH tunnel is disconnected because $tunnel went out of scope.

function connect(): void
{
    return new SshTunnel(...);
}

$tunnel = connect();
// At this point the SSH tunnel works.