Jump to content

How to flush console output line by line to html webpage ?


kkmoslehpour

Recommended Posts

I am working on outputting the console output to the user line by line. I have a page and based on user selection (radio buttons) it will run each script. I was looking at the PHP docs (http://php.net/manual/en/function.ob-flush.php) and there were saying that we have to call flush(); and ob_flush(); together to get the output to the webpage as it outputs to the console. I tried to do that but not getting any luck. I also tried adding the header( 'Content-type: text/html; charset=utf-8' ); Also no luck.

I have the following function that can successfully output the stream, but it has to wait until the console is finished outputting to stdout before it displays on the webpage. I want it to output real-time. Is there a way to do this? Or am I doing something wrong? 

 

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


    }
    }   

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


    }
    }   

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.