savvii/ssh-tunnel

创建SSH隧道的小型库,例如用于MySQL

1.1.2 2023-03-09 15:58 UTC

This package is auto-updated.

Last update: 2024-09-18 01:39:01 UTC


README

Code Quality

创建SSH隧道的小型库

示例用法

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
    )
);

当PHP脚本结束或SshTunnel对象被销毁时,SSH隧道将断开。

警告

当你创建对象但让它超出作用域时,默认情况下SSH隧道将被清理。这将不起作用

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.

如果你在类的构造函数中创建SshTunnel对象,请确保将其存储在类属性中,以确保在构造函数完成后不会超出作用域。

要求

  • Linux、MacOS或FreeBSD
  • PHP 8.0或更高版本
  • 启用了PHP函数proc_openproc_closeproc_terminateproc_get_status
  • 二进制文件ssh
  • 二进制文件lsof,默认使用,但可以跳过。
  • 二进制文件nc,默认使用,但可以跳过。