我想在远程机器上执行一个命令,并使用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
已经安装,并且没有返回错误
我猜您的命令不正确:
$command = 'whoami';
您还应该将这2行添加到末尾以获得输出:
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);
}