简体   繁体   中英

PHP shell_exec does not work with ssh

Shell_exec works correctly in PHP, but when using ssh it does not return any output..

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

The above command works fine in a bash shell and the following displays the proper output in PHP

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

I was hoping this could be done without using a third party php library...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM