我想在远程计算机上执行命令,并使用php
将该命令存储在变量中这是我试过的
$command = 'exec("whoami")';
$connection = ssh2_connect($ip,$port);
ssh2_auth_password($connection,$user,$pass);
$test = ssh2_shell($connection,$command);
echo $test;
根据我的$ test应输出root
然而,没有任何回报,我相信我错过了一些.....
已安装php-pecl-ssh2
且未返回任何错误
答案 0 :(得分:1)
我猜你的命令不正确:
$command = 'whoami';
你还应该将这两行添加到最后以获得输出:
if ( $connection = ssh2_connect($ip,$port) ) {
echo 'Error occured while connecting to server via ssh';
}
if (!ssh2_auth_password($connection,$user,$pass)) {
echo 'Error occured while authenticating via ssh';
}
if(!$test = ssh2_shell($connection,$command)){
echo 'Error occured while executing remote command via ssh';
} else {
stream_set_blocking($test, true);
echo stream_get_contents($test);
}