如何使用 ajax 在 php 页面中显示运行时 shell 脚本的输出



我有两个php页面。 第一页将在客户端浏览器和显示按钮上加载。如果单击它将使用 ajax 调用来执行我的第二个 php 页面。第二个 php 页面使用 ssh2_exec 连接到服务器,执行一些 shell 脚本并在同一个加载页面上显示输出而无需刷新。我能够用我现有的页面实现这一点。但它会在第 2 页完成执行后立即显示完整的输出。我希望当第二个 php 页面开始执行时,输出应该开始在客户端浏览器上逐行显示。

以下是我的 2 页:

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 :

<?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);
?>

代码的问题在于,使用 AJAX 时,您无法获取可用的数据,只能在服务器端脚本关闭连接后获取完整的响应。

一种解决方案是使用服务器发送的事件

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);
}

您的 PHP 脚本还需要修改:

header("Content-Type: text/event-streamnn");
$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: commandStartedn";
while($line1 = fgets($script_output1)) {
echo 'data: ' . json_encode($line1) . "nn";
ob_flush();
flush();
}          
echo "event: commandEndedn";
} else {
echo 'data: "Already one pending script running in selected server. Kindly wait!!!"' . "nn"; 
}
fclose($script_output1);
ssh2_exec($conn, 'exit');
unset($conn);

最新更新