Live Output w/ Remote ssh2_exec()?

I’m trying to set up an interface between a PHP page and a Red Hat server. All the page does is send Shell commands to the app server and what I’m needing to do is present the user with live output as each command executes.

What I’m thinking is this: I’d connect to the remote machine using something like $connection = ssh2_connect(‘www.whatever.com’, 22) and then use that connection to issue something similar to popen(). The problem is that I’m just not sure how to go about doing this based on the few examples I’ve been able to find on the internet…

An example of the kind of functionality I’m after is as follows:

<?php
    header('Content-Encoding: none;');

    set_time_limit(0);

    $handle = popen("ping 127.0.0.1", "r");

    if (ob_get_level() == 0) 
        ob_start();

    while(!feof($handle)) {

        $buffer = fgets($handle);
        $buffer = trim(htmlspecialchars($buffer));

        echo $buffer . "<br />";
        echo str_pad('', 4096);    

        ob_flush();
        flush();
        sleep(1);
    }

    pclose($handle);
    ob_end_flush();
?>

…only with other commands more in-depth than ping. :slight_smile:

(If you take the code above and run it in the browser, you can see the output as it happens–which is very cool… I just wish I knew how to apply that same approach with a remote server connection with ssh2_exec().)

Thoughts?

I thought I’d post this in case anyone on here is going down a similar path…

In doing some more research and posting to another forum (PHP Developers Network), I found a way to get a little closer to what I’m after:

[FONT=monospace][COLOR=#000000][B]<?php[/B][/COLOR]

    //SFTP connection details to an app server...
    [COLOR=#000088]$domain[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'<whatever>'[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000088]$user[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'<username>'[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#000088]$pass[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#0000ff]'<password>'[/COLOR][COLOR=#339933];[/COLOR]

    [COLOR=#990000]header[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]'Content-Encoding: none;'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    [COLOR=#990000]set_time_limit[/COLOR][COLOR=#009900]([/COLOR]0[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]

    [COLOR=#000088]$connection[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#990000]ssh2_connect[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$domain[/COLOR][COLOR=#339933],[/COLOR] 22[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
    
        [COLOR=#b1b100]if[/COLOR][COLOR=#009900]([/COLOR][COLOR=#990000]ssh2_auth_password[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$connection[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000088]$user[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#000088]$pass[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900]{[/COLOR][COLOR=#666666][I]//If authentication is successful...[/I][/COLOR]
                [COLOR=#666666][I]//$h = ssh2_exec($connection, 'ls');[/I][/COLOR]
                [COLOR=#000088]$h[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#990000]ssh2_exec[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$connection[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000ff]'man ping'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                [COLOR=#000088]$out[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#990000]ssh2_fetch_stream[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$h[/COLOR][COLOR=#339933],[/COLOR] SSH2_STREAM_STDIO[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]

                [COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#990000]ob_get_level[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]==[/COLOR] 0[COLOR=#009900])[/COLOR]
                  [COLOR=#990000]ob_start[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]

                [COLOR=#b1b100]while[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#339933]![/COLOR][COLOR=#990000]feof[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$out[/COLOR][COLOR=#009900])[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
                  [COLOR=#000088]$line[/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#990000]fgets[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$out[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                  [COLOR=#b1b100]echo[/COLOR] [COLOR=#000088]$line[/COLOR][COLOR=#339933].[/COLOR][COLOR=#0000ff]'<br />'[/COLOR][COLOR=#339933];[/COLOR]
                  [COLOR=#b1b100]echo[/COLOR] [COLOR=#990000]str_pad[/COLOR][COLOR=#009900]([/COLOR][COLOR=#0000ff]''[/COLOR][COLOR=#339933],[/COLOR] 4096[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                  [COLOR=#990000]ob_flush[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                  [COLOR=#990000]flush[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                  [COLOR=#990000]sleep[/COLOR][COLOR=#009900]([/COLOR]1[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                [COLOR=#009900]}[/COLOR]

                [COLOR=#990000]fclose[/COLOR][COLOR=#009900]([/COLOR][COLOR=#000088]$out[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
                [COLOR=#990000]ob_end_flush[/COLOR][COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
        [COLOR=#009900]}[/COLOR]
        
[COLOR=#000000][B]?>[/B][/COLOR]
[/FONT]

The only issue here is that I think it’s pretending to act as if live output is being displayed regardless of how long the actual issued command takes to resolve against the server because everything is displayed after 1 second per-line due to sleep()… So I’m curious if anyone on here has any idea where I’m going wrong? I’d love some insight! :slight_smile: Also, just out of curiosity, why is the man output displaying so oddly? I.e. - duplicate letters and such? Is it an encoding issue? If so, what encoding should I use and should that be used across the board with everything I issue against the server?

Thanks and I’ll follow-up if I find anything else out about what I’m trying to do here (I figure maybe someone out there might find it useful, worth learning about, etc.).

You might do better by “polling” instead of refreshing.

i.e. Have the page check to see if there is anything new. Then and only then fetch it and manipulate the DOM accordingly.

At this point, I’m just trying to nail down how to display live results (as-they-occur) from the commands that get sent over to a RHEL app server. :slight_smile: