简体   繁体   English

如何使用Ajax在PHP页面中的运行时显示Shell脚本的输出

[英]How to display output of shell script at runtime in php page using ajax

I have two php pages. 我有两个PHP页面。 1st page will load at client browser and display buttons. 第一页将加载到客户端浏览器并显示按钮。 If clicked it will use ajax call to execute my second php page. 如果单击,它将使用ajax调用执行我的第二个php页面。 The 2nd php page connects to a server using ssh2_exec, execute some shell scripting and display the output on the same loaded page without refresh. 第2个php页面使用ssh2_exec连接到服务器,执行一些shell脚本,并在同一页面上显示输出而无需刷新。 I am able to achieve this with my existing pages. 我可以通过现有页面实现这一目标。 But it displays complete output at once after 2nd page completes its execution. 但是在第二页完成执行后,它将立即显示完整的输出。 I want as the 2nd php page start execution the output should start displaying line by line on client browser. 我想作为第二个php页面开始执行,输出应开始在客户端浏览器上逐行显示。

Below are my 2 pages: 以下是我的2页:

php page1: php第1页:

<!DOCTYPE html>
<head>
<link rel="stylesheet" href="../css/new_style.css" />
</head>
<script>
function disable()
{
document.getElementById("save").disabled = true;
}

function enable_button()
{
document.getElementById("save").disabled = false;
if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   document.getElementById('output').innerHTML = xmlhttp.responseText;
 }
}

xmlhttp.open('GET', 'arg0_orgcmd_exec.php?id=118' , true);
xmlhttp.send();
}


function disable_button()
{
document.getElementById("execute").disabled = true;
   }

</script>


<body>

<div class="main_body">

<form action=" " method="" name="output_frame">
<center>
<input class=" " id="execute" onclick="enable_button();" type="button" name="submit" value="Execute" />
<input class=" " disabled name="save"  id="save" type="submit"  onclick="disable_button();" value="Save In File" />
<input class=" btn btn-danger back-btn" type=button onclick="location.href='main_menu.php'" value='Close' />
</center>
</form>
</div>

<div id="output" class="output">
</div>
<iframe name="iframe_output"></iframe>

</body>
</html>

arg0_orgcmd_exec.php : arg0_orgcmd_exec.php:

<?php
$page_id = $_GET['id'];

$conn = ssh2_connect($server_ip,$server_port);
ssh2_auth_password($conn, $server_id, $server_pass);

//Checking web.lock file
$stream_lock =  ssh2_exec($conn, "ls /tmp/web.lock" );
stream_set_blocking($stream_lock, true);
$check_lock = stream_get_contents($stream_lock);
if(empty($check_lock)) {
            $script_output1 = ssh2_exec($conn, "touch /tmp/web.lock ;  my_script.sh ; rm -rf /tmp/web.lock" );
        stream_set_blocking($script_output1, true);
        echo "<pre>";
        while($line1 = fgets($script_output1)) {
        echo $line1;
        ob_flush();
        flush();

               }
        echo "</pre>";
                                echo "<br />";
                                echo "<h2>Script Output Finished!!!!<h2>";
                                echo "<br />";
}else {echo "Already one pending script running in selected server. Kindly wait!!!"; }



fclose($script_output1);
ssh2_exec($conn, 'exit');
unset($conn);

?>

The problem with your code is that with AJAX you can't get the data as it is available, only the complete response after the server side script closes the connection. 代码的问题在于,使用AJAX时,您无法获取可用的数据,只能获取服务器端脚本关闭连接后的完整响应。

One solution is to use Server Sent Events 一种解决方案是使用服务器发送事件

var evtSource = new EventSource("arg0_orgcmd_exec.php?id=118");
evtSource.onmessage = function(e) {
  var newElement = document.createElement("div");

  newElement.innerHTML = "<br />message: " + e.data;
  document.getElementById('output').appendChild(newElement);
}

Your PHP script needs modifications also: 您的PHP脚本也需要修改:

header("Content-Type: text/event-stream\n\n");
$page_id = $_GET['id'];

$conn = ssh2_connect($server_ip,$server_port);
ssh2_auth_password($conn, $server_id, $server_pass);

//Checking web.lock file
$stream_lock =  ssh2_exec($conn, "ls /tmp/web.lock" );
stream_set_blocking($stream_lock, true);
$check_lock = stream_get_contents($stream_lock);
if(empty($check_lock)) {
        $script_output1 = ssh2_exec($conn, "touch /tmp/web.lock ;  my_script.sh ; rm -rf /tmp/web.lock" );
        stream_set_blocking($script_output1, true);
        echo "event: commandStarted\n";

        while($line1 = fgets($script_output1)) {
             echo 'data: ' . json_encode($line1) . "\n\n";
             ob_flush();
            flush();
       }          
       echo "event: commandEnded\n";
} else {
     echo 'data: "Already one pending script running in selected server. Kindly wait!!!"' . "\n\n"; 
}

fclose($script_output1);
ssh2_exec($conn, 'exit');
unset($conn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM