divineomega/php-ssh-connection

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

v2.2.0 2020-05-27 22:58 UTC

This package is auto-updated.

Last update: 2024-08-28 08:59:43 UTC


README

Build Status Coverage Status

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

安装

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

composer require divineomega/php-ssh-connection

用法

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

$connection = (new SSHConnection())
            ->to('test.rebex.net')
            ->onPort(22)
            ->as('demo')
            ->withPassword('password')
         // ->withPrivateKey($privateKeyPath)
         // ->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);