2

I've been working on a way to display ssh output in my web browser. What I have is a shell script I've created which does a tail -f /var/log/syslog with some grep arguments.

Now I wish to call this shell script and display the data returned in real time. What I have is a ajax request that calls a php function

public function siptrace($num) {

if(is_numeric($num)) {

    if($ssh = ssh2_connect('127.0.0.1', 3972)) {
        if(ssh2_auth_password($ssh, 'user', 'password')) {
            $stream = ssh2_exec($ssh, 'script ');
            stream_set_blocking($stream, true);
            $data = '';
            sleep(1);
            while($line = fgets($stream)) {
                flush(); 
                echo $line . "<br>";
            }
        }
    }

}

}

I have verified connectivity by running simple commands such as ls. But now when I am trying to grep data in real time it does not return anything. I can see the ajax call loading and loading, which it should do because of the while and shell structure?

Ajax

            $(document).ready(function() {

                $('#sub').click(function() {

                    $.ajax({ url: 'load.php?trace='+$('#num').val(), success:function(data){

                    } });

                });

            });

Are anyone noticing anything wrong with my code?

Ynhockey
  • 3,845
  • 5
  • 33
  • 51
anonamas
  • 243
  • 1
  • 6
  • 15

1 Answers1

1

Try this instead:

$ssh = new Net_SSH2('127.0.0.1', 3972);
$ssh->login('user', 'password');
$ssh->setTimeout(2);
$ssh->enablePTY();
echo $ssh->exec('script ');

That uses phpseclib, a pure PHP SSH2 implementation. Might work better for you than libssh2.

taulia
  • 13
  • 2