librasoft-fr/php-ssh-connection

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

3.1.0 2022-10-28 20:52 UTC

This package is auto-updated.

Last update: 2024-08-29 00:59:08 UTC


README

Build Status Coverage Status

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

安装

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

composer require librasoft-fr/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);