等待命令完成使用PHP中的SSH2_Shell [英] Waiting for command to finish using SSH2_Shell in PHP
问题描述
我有一个在PHP中运行的SSH2_Shell会话.我的问题是,我需要一个命令才能完全完成,然后再移至下一个命令.到目前为止,这是我的代码:
I have an SSH2_Shell session working in PHP. my issue is that i need a command to completely finish before moving onto the next command. Here is my code so far:
$command_capture = "cd /mnt/NADS/scripts/";
$command_capture2 = "./tcpdump.sh $capture_name $sleep";
if (!($connection = ssh2_connect("172.20.1.18", 22))) {
echo "fail: unable to establish connection";
}
if (!ssh2_auth_password($connection, "root", "Hideandseek")) {
echo "fail: unable to authenticate";
}
$stream = ssh2_shell($connection);
fwrite($stream, $command_capture. PHP_EOL);
sleep(1);
fwrite($stream, $command_capture2 . PHP_EOL);
sleep(5);
$data="";
while($buf = stream_get_contents($stream)){
$data.=$buf;
}
echo $data;
fclose($stream);
tcpdump.sh脚本正在运行lftp命令,但是没有足够的时间来完成.我不能使用睡眠,因为它可能需要比指定的时间更长的时间,如果只需要几秒钟,我就不想让用户等待.我没有像我那样实现stream_set_blocking的运气,它似乎冻结了我的浏览器.
the tcpdump.sh script is running a lftp command but is not being given anough time to complete. I cant use sleep as it may take longer then the specified time and i dont want to make the user wait if it only needs a few seconds. I have not had luck implementing stream_set_blocking as when i do, it seems to freeze up my browser.
总的来说,我需要一种方法来检测命令何时完成并移至下一个命令.
overall, I need a way to detect when a command has finished and move into the next command.
提前谢谢!
推荐答案
感谢这些想法,但我想我已经解决了.当命令完成时,我回显了一些特殊字符,然后我将使用strpos()搜索这些字符.我认为可能有帮助的另一件事是调整php.ini文件中的max_execution_time设置.我在这里找到了答案:
Thanks for the ideas but I think I got it solved. I did an echo of a few special characters when the command finished and then I would search for the characters using strpos(). Another thing I think may have helped was adjusting the max_execution_time setting in the php.ini file. I found that answer here:
http://verysimple.com/2006/03/30/fatal-error-maximum-execution-time-of--30-seconds-exceed/
这是我的新代码
$data="";
while (true){
$data .= stream_get_contents($stream);
if (strpos($data,"XOXO") !== false) {
echo "okay: command finished\n";
break;
}
}
echo $data;
fclose($stream);
这篇关于等待命令完成使用PHP中的SSH2_Shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!