0

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.

KevinO
  • 4,303
  • 4
  • 27
  • 36
WiththeWind
  • 153
  • 1
  • 9

3 Answers3

0

The timeout you are referring to is most likely a script execution time limit [very common on shared hosting]

And there is not much you can do about that sadly.

However what you can do is [if you have control over the server where the script is called from] is

  1. send the request to the remote server [including a webhook callback url]
  2. Have the script do it's thing
  3. Have the script run the webhook to do the processing of the results

offcourse this also has implications on how the processing/displaying should be handled but i do not have enough information to go into specifics in this answer.

YouriKoeman
  • 768
  • 3
  • 10
0

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.

Marco
  • 335
  • 2
  • 9
  • Just mark as accepted answer please, though the webhook idea from Youri as also a good answer, but more complex (imho). – Marco Oct 21 '18 at 11:22
0

Do not attempt to execute long running scripts in a web page.

If you need a response from another system and you have SSH access then seperate the invocation of the task and the collection of the results into 2 seperate steps (see link above and the discussion it links to for some hints on how to do the invocation). Put a timed redirect on the first page to the second.

symcbean
  • 47,736
  • 6
  • 59
  • 94