Search code examples
phpssh2-exec

PHP Long Running Script timeout


I am creating a page that executes a shell script on a remote server to scan a website and outputs the results on the screen. The output can sometimes take awhile to get depending on the size of the site being scanned. Currently the script works and does what it's supposed to but the problem is when I scan larger sites it stalls and on the platform the website is being hosted on has a timeout of 30 seconds that I cannot alter.

I am wondering what the best way to keep the connection alive whether it just be sending dots to the screen or maybe something else just to keep the connection alive.

Here is my script

$ssh = new Net_SSH2('hostname');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

$ansi = new File_ANSI();
$ssh->enablePTY();
$ssh->setTimeout(60);
$ssh->exec("./test.sh | awk 'NR >= 16 {print}'\n");
$ansi->appendString($ssh->read());
echo $ansi->getHistory();

Any help or guidance is deeply appreciated.


Solution

  • You should rather let the page load and e.g. run an AJAX request that will wait for a reply/listen on a port than trying to keep the connection alive.

    So on the user's side, it would run an ajax request (javascript) to the php url, then on success you display the result.

    $.ajax({
      url: "/thescript.php":,
      type: "POST",
      datatype: "POST"
      success: function(){
        //do display stuff
      }
    
    });
    

    Would probably add a reasonable timeout.