从远程服务器bash脚本到html页面逐行输出流

时间:2017-08-30 02:18:16

标签: javascript php bash

我试图逐行将我的bash脚本中的流输出到我的html页面。我一直在寻找如何做到这一点。目前,我的代码在下面工作,但是,它将等到我的bash脚本完成之后才将整个内容输出到页面上,大约需要几分钟。我希望用户逐行查看输出实时,以便他们知道发生了什么。基本上,用户所做的是,他们单击网页上的单选按钮并启动myFirstScript.sh或mySecondScript.sh,该脚本将在远程服务器上运行并将流输出回用户。我无法使用下面的代码,如果有人知道某种方式请告诉我。

myPage.html下

<!DOCTYPE html>
<html>
<head>
    <style>
    .box1 form {
        width: 450px;
        border: 2px solid black;
        margin: 0;
        display: flex;
    }

    .col {
        display: inline-block;
        border-right: 2px solid black;
        padding: 5px;
        width: 200px;
    }

    .col:last-child {
        border-right: none;
    }

    input[type=text], select {
            width: 20%;
            padding: 12px 20px;
            margin: 8px 0;
            display: inline-block;
                border: 1px solid #ccc;
                border-radius: 4px;
                box-sizing: border-box;
                background-color: #ffffff;
        }
    </style>
</head>
<body>
<div id="gatewayInput">
  <form> 
      <input type="text" id="gateway" name="gateway" action="testexe.php" method="POST" placeholder="Gateway Name"><br><br>
  </form>
</div>
<div class="box1">
<form method="post">
<label class="col">Up/Down</label>
<span class="col">
  <input type="radio" name="option" id="r1" value="1" />
  <label for="r1">Up</label>
  <input type="radio" name="option" id="r2" value="2" />
  <label for="r2">Down</label> 
</span>
<span class="col">
  <input type="submit" class="button"/>
</span>
</form>
</div>
    <script src ="../../../jqueryDir/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
    $(".button").click(function(event){
        if ((document.getElementsByName("gateway")[0].value == '')) {
               alert('Gateway Required!');
        return false;
    }
        else if (document.querySelectorAll('input[type="radio"]:checked').length < 1) {
               alert('Please Choose Up/Down Value!');
               return false;
        } 
        else {
               //alert('Sucess!');
            event.preventDefault();
            $.ajax({
            url:"testexe.php",
            type: "POST",
                    data: {option: $('input[type=radio]:checked').val()},
            dataType: "text", 
            success:function(result){
                        $('#div1').html(result)
            }
            });
             return true;
        }
  });
    </script>
<div id="div1"></div>
</body>
</html>

testexe.php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
 if ($connection = @ssh2_connect($gateway, 22)) {
           ssh2_auth_password($connection, $gwUser, $gwPwd);
           if(isset($_POST['option']) && $_POST['option'] == 1) { 
                $stream = ssh2_exec($connection, "/home/mydirectory/myFirstScript.sh");
                stream_set_blocking($stream, true);
                $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
                $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
                while($line = fgets($stream_out)) {flush(); ob_flush(); echo '<pre>' . $line . '</pre>';}
                echo '<pre>' . "------------------------\n" . '</pre>';
                while($line = fgets($stream_err)) {flush(); ob_flush(); echo '<pre>' . $line . '</pre>';}
                fclose($stream);

           }

           if(isset($_POST['option'])  && $_POST['option'] == 2) { 
                $stream = ssh2_exec($connection, "/home/mydirectory/mySecondScript.sh");
                stream_set_blocking($stream, true);
                $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
                $stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); 
                while($line = fgets($stream_out)) {flush(); ob_flush(); echo '<pre>' . $line . '</pre>';}
                echo '<pre>' . "------------------------\n" . '</pre>';
                while($line = fgets($stream_err)) {echo '<pre>' . $line . '</pre>';ob_flush();flush();}
                fclose($stream);
           }

     }
}   

myFirstScript.sh

#!/bin/bash

echo 'expect -f ./scripts/thisIsMyExpectscript.exp'  #this will call another script and output to the console
expect -f ./scripts/thisIsMyExpectscript.exp         #this will call another script and output to the console
echo

mySecondScript.sh

#!/bin/bash

for (( c=1; c<=1000000; c++ ))
do
   echo 'Welcome' $c 'times'
done
echo

0 个答案:

没有答案