Поток вывода из сценария bash удаленного сервера построчно на html-страницу

Я пытаюсь выводить поток из моего скрипта 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 ответов

Другие вопросы по тегам