gpt4 book ai didi

php - 在远程计算机上执行并存储输出

转载 作者:行者123 更新时间:2023-12-02 14:04:06 26 4
gpt4 key购买 nike

我想在远程计算机上执行命令,并使用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';

您还应该在最后两行添加以下内容以获得输出:
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);
}

关于php - 在远程计算机上执行并存储输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22971282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com