oncesk / ssh2
此包的最新版本(dev-master)没有可用的许可证信息。
ssh2 客户端库
dev-master
2014-06-19 08:42 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-24 07:11:57 UTC
README
提供执行远程命令(如终端中)能力的库
使用此库,您可以使用 ssh2 协议执行任何命令。
####使用方法
- 密码认证
<?php use Ssh\Client; use Ssh\Auth\Password; $auth = new Password('username', 'password'); $client = new Client('host_name_or_ip'); try { $client->connect()->authenticate($auth); echo $client->exec('pwd'); } catch (\RuntimeException $e) { echo $e->getMessage(); }
- 公钥认证
<?php use Ssh\Client; use Ssh\Auth\PublicKey; $auth = new PublicKey('username', 'path to public key', 'path to private key', 'passphrase if set'); $client = new Client('host_name_or_ip'); try { $client->connect()->authenticate($auth); echo $client->exec('pwd'); } catch (\RuntimeException $e) { echo $e->getMessage(); }
- 命令链
<?php use Ssh\Client; use Ssh\Auth\PublicKey; use Ssh\Command\Result; use Ssh\Command\Chain; $auth = new PublicKey('username', 'path to public key', 'path to private key', 'passphrase if set'); $client = new Client('host_name_or_ip'); try { $client->connect()->authenticate($auth); echo $client ->chain() ->exec('pgrep node', function (Result $result, Chain $chain) { $result = $result->getResult(); if ($result && is_numeric($result)) { $chain->stopChain(); } }) ->exec('/usr/local/bin/node ~/server.js > ~/node_server.log &'); } catch (\RuntimeException $e) { echo $e->getMessage(); }
- 使用 ssh 配置文件
<?php use Ssh\Config\Configuration; use Ssh\Client; $configuration = new Configuration('path to ssh config file like a /home/username/.ssh/config'); $client = new Client($configuration->getHost('hostname')); // now if you need authenticates with password (PasswordAuthentication set to yes) you need to set password $client->getAuth()->setPassword('xxxxx'); // else if you set IdentityFile or PubkeyAuthentication is yes or not set PubkeyAuthentication // trying to find currenct user public key and private key files into ~/.ssh/ directory, if keys not exists // you need authenticates with methods above, if keys exists you can connect and authenticate try { $client->connect()->authenticate(); echo $client->exec('pwd'); } catch (\RuntimeException $e) { echo $e->getMessage(); }
- Shell
使用 Shell,您可以在终端中工作