Shell_exec在PHP中可以正常工作,但是在使用ssh时不会返回任何输出。
<?php
echo shell_exec("/usr/bin/ssh -i /tmp/key server 'ls'");
?>
上面的命令在bash shell中工作正常,下面的命令在PHP中显示正确的输出
<?php
echo shell_exec("ls");
?>
我希望可以在不使用第三方php库的情况下完成此操作...
最佳答案
使用phpseclib, a pure PHP SSH2 implementation:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('/tmp/key'));
if (!$ssh->login('username', $key)) {
exit('Login Failed');
}
echo $ssh->exec('ls');
?>
关于php - PHP shell_exec不适用于ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16087377/