我正在尝试在 PHP 中运行 CLI 命令,但在不同的服务器上。为了在另一台服务器上运行命令,我使用的是linuxssh
命令。为了在 PHP 中运行 CLI 命令,我使用exec()
.
这有效...
$output = exec('cut -d: -f1 /etc/passwd'); //works!
这可以从一台服务器的命令行运行到另一台服务器......
ssh my.otherserver.com "date"
但是,当我尝试在 PHP 中执行此操作时,它不起作用......
$output = exec('ssh my.otherserver.com "date"'); //does not work!
我想做的事情可能吗?我该怎么做呢?提前致谢。
ssh2_connect 或 phpseclib
您可以使用 ssh2_connect 或 phpseclib。在我的 EC2 实例上,我必须安装 ssh2_connect 的软件包(例如 yum install php56-pecl-ssh2-0.12-5.15.amzn1.x86_64),因为默认情况下它不存在。注意:这也可以使用 bash 脚本来完成。
<?php
//ssh2_connect:
$connection = ssh2_connect('localhost', 22);
ssh2_auth_password($connection, 'user', 'password');
$stream = ssh2_exec($connection, 'ls -l > /tmp/worked.txt');
//phpseclib:
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.example.com');
$ssh->login('username', 'password') or die("Login failed");
echo $ssh->exec('command');
进一步阅读:
http://phpseclib.sourceforge.net/ http://phpseclib.sourceforge.net/
http://php.net/manual/en/function.ssh2-connect.php http://php.net/manual/en/function.ssh2-connect.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)