2013-04-18 124 views
1

了shell_exec正常工作在PHP,但使用ssh當它不返回任何輸出..PHP了shell_exec不使用ssh工作

<?php 
    echo shell_exec("/usr/bin/ssh -i /tmp/key server 'ls'"); 
?> 

上面的命令工作正常在bash shell和以下顯示正確在PHP輸出

<?php 
    echo shell_exec("ls"); 
?> 

我希望這可以在不使用第三方PHP庫來完成...

回答

1

使用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'); 
?>