2017-03-22 9 views
0

私は2つのPHPページを持っています。 1ページ目はクライアントブラウザと表示ボタンに表示されます。クリックすると、ajax呼び出しを使用して2番目のPHPページが実行されます。 2番目のphpページはssh2_execを使用してサーバに接続し、いくつかのシェルスクリプトを実行して、読み込まれた同じページに出力を更新せずに表示します。私は既存のページでこれを達成することができます。しかし、2ページ目の実行が完了してすぐに完全な出力を表示します。私は2番目のPHPページの実行がクライアントブラウザ上で行ごとに表示を開始する必要が実行を開始したい。実行時のシェルスクリプトの出力をajaxを使ってPHPページに表示するには

PHPのページ1:以下

は私の2ページです

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

?> 

答えて

0

あなたのコードの問題は、AJAXとあなたが得ることができないということですデータが利用可能になると、サーバーサイドスクリプトが接続を終了した後の完全な応答のみが返されます。

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

一つの解決策は、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); 
} 

あなたのPHPスクリプトも修正を必要とする使用することです

関連する問題