在 PHP 中使用 SSH2_Shell 等待命令完成
我有一个在 PHP 中工作的 SSH2_Shell 会话。我的问题是,我需要一个命令才能完全完成,然后才能继续执行下一个命令。到目前为止,这是我的代码:
$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,它似乎冻结了我的浏览器。
总的来说,我需要一种方法来检测命令何时完成并进入下一个命令。
提前致谢!
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);
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.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的想法,但我想我已经解决了。命令完成后,我对一些特殊字符进行了回显,然后使用 strpos() 搜索这些字符。我认为可能有帮助的另一件事是调整 php.ini 文件中的 max_execution_time 设置。我在这里找到了答案:
http://verysimple.com/2006/03/30/fatal-error-maximum-execution-time-of-30-seconds-exceeded/
这是我的新代码
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-exceeded/
And here is my new code
我有一个在 github 上实现 Secure Shell2 的 SSH2 类。查看 https://github。 com/bubba-h57/PHP-SSH2 看看是否有帮助。
不过,我仍在继续寻找更好的方法来处理查找提示,因此任何建议或贡献都将受到欢迎。希望它能有所帮助,哪怕只是给你一些想法。
I have an SSH2 class that implements the Secure Shell2 over on github .. check out https://github.com/bubba-h57/PHP-SSH2 and see if that helps.
I continue to search for a better way to handle finding the prompts though, so any advice or contribution to that would be welcomed. Hopefully it will help, if nothing more than giving you some ideas.
您可以使用 phpseclib,一个纯 PHP SSH 实现。例如。
如果您确实需要 shell 支持,您可以执行以下操作(使用最新的 SVN):
You could use phpseclib, a pure PHP SSH implementation. eg.
If you really need shell support you could do the following (with the latest SVN):