[英]Real-time SSH output in Laravel with phpseclib
I've tried different SSH libraries but they don't seem to work too well in Laravel. 我尝试了不同的SSH库,但是它们在Laravel中似乎不太好用。 Since I got phpseclib to work well, I'm trying to stick with it. 由于我使phpseclib正常工作,因此我尝试坚持使用它。 But, I'm not seeing a way to have a live output from the SSH connection. 但是,我没有从SSH连接获得实时输出的方法。
Some things that I plan on running can take up to a few hours to finish but the software being accessed always prints out a percentage of completion that I'd like to utilize to display in the browser. 我计划运行的某些事情可能要花几个小时才能完成,但是所访问的软件始终会打印出我想用来显示在浏览器中的完成百分比。
Currently, I have this: 目前,我有这个:
<?php
use phpseclib\Net\SSH2;
use phpseclib\Crypt\RSA;
$key = new RSA();
$key->loadKey(file_get_contents('key.pem'));
$command = 'ping google.com';
$ssh = new SSH2('awesomeserver.com');
if (!$ssh->login('username', $key))
{
echo 'Login Failed';
}
else
{
echo $ssh->exec($command);
}
?>
This just waits until the command is complete and then just barfs it all onto the screen. 这只是等待命令完成,然后将其全部倒入屏幕。
Previously, in the else
bracket, I had while(@ ob_end_flush()); 以前,在else
括号中,我有while(@ ob_end_flush());
$proc = $ssh->exec($command);
while(!feof($proc))
{
echo fread($proc, 4096);
@ flush();
}
But, $proc
was only recognized as a string instead of a resource, so it didn't work. 但是, $proc
仅被识别为字符串而不是资源,因此它不起作用。
Do you all have any other suggestions, other than using a different library? 除了使用其他库以外,您还有其他建议吗?
Thanks! 谢谢!
EDIT (Partial solution): 编辑(部分解决方案):
I eventually got this script to work by utilizing the read()
function with this: while(@ ob_end_flush()); 我最终通过利用read()
函数与此脚本一起工作:while(@ ob_end_flush());
$ssh->write($command.'\n');
while($read = $ssh->read())
{
echo $read;
@ flush();
}
You could employee a callback. 您可以雇用一个回调。 eg. 例如。
$ssh->exec('ping 127.0.0.1', function($output) {
echo $output;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.