pdonatas/php-ssh-connection

提供优雅的语法连接到SSH服务器并执行命令。

v2.2.1 2023-11-01 19:27 UTC

This package is auto-updated.

Last update: 2024-09-30 01:46:50 UTC


README

由于原始仓库(https://github.com/DivineOmega/php-ssh-connection)已弃用,我将继续在这个分支上提供支持。

PHP SSH连接包提供优雅的语法连接到SSH服务器并执行命令。它支持密码和公私钥对认证,并可以轻松捕获命令输出和错误。

安装

您可以通过运行以下Composer命令来安装PHP SSH连接包。

composer require pdonatas/php-ssh-connection

使用方法

请参阅以下基本使用说明。

$connection = (new SSHConnection())
            ->to('test.rebex.net')
            ->onPort(22)
            ->as('demo')
            ->withPassword('password')
         // ->withPrivateKey($privateKeyPath)
         // ->withPrivateKeyString($privateKeyString)
         // ->timeout(0)
            ->connect();

$command = $connection->run('echo "Hello world!"');

$command->getOutput();  // 'Hello World'
$command->getError();   // ''

$connection->upload($localPath, $remotePath);
$connection->download($remotePath, $localPath);

为了安全起见,您可以指纹远程服务器并在每次后续连接时验证指纹是否保持不变。

$fingerprint = $connection->fingerprint();

if ($newConnection->fingerprint() != $fingerprint) {
    throw new Exception('Fingerprint does not match!');
}

如果您愿意,您可以指定要检索的指纹类型。

$md5Fingerprint  = $connection->fingerprint(SSHConnection::FINGERPRINT_MD5); // default
$sha1Fingerprint = $connection->fingerprint(SSHConnection::FINGERPRINT_SHA1);