开发者

Abort a PHP script trying to connect through SSH with an Ajax request?

开发者 https://www.devze.com 2023-03-19 11:16 出处:网络
Bonjour, I\'m a trying to connect to a router with PHP and phpseclib. <?php function connect_ssh(){ set_time_limit(10);

Bonjour, I'm a trying to connect to a router with PHP and phpseclib.

<?php
    function connect_ssh(){

    set_time_limit(10);
    ob_start();

    register_shutdown_function('shutdown');

    if(connection_aborded()){
       die();
    }


    function shutdown(){
       print ob_get_clean();
    }

       $res = array();
       $a = new Net_SSH2($host);

       $a->write("show time\r\n");


     try{
        $res[] = $a->read('\r\n');
        $res[] = $a->read('\r\n');
        $res[] = $a->read('#');

     } catch(Exception $e)
     {    

          ob_flush();
          var_dump(compact('e','res'));
     }


    } return json_encode(compact('e','res'));?>

Im calling this function using jQuery:

$.ajax({
 url:'myURL',
 data: null,
 type: 'POST',
 dataType: 'json',
 success: function(z){ console.log(z);}
});

I would like to stop my php script on demande because sometimes, the "Net_SSH2::read()" failed if the parameter is not found in the routeur answer. (i.e if there is a syntaxe error).

The more often, I am not able t开发者_StackOverflowo reconnect to my web server after a failed request and I have to wait for a long time or even reboot Apache...

Thanks for your help...


Make it check a database in a loop and post the AJAX bit to another script that just inserts a quit value into the database.

EDIT: I'm not sure if I completely understand your code, but here's what I would do:

function() {
    $con = database_of_choice($host, $user, $pass, $db);
    while (!$quitter) {
        $r = $con->query('SELECT shouldquit FROM info LIMIT 1');
        if ($r['shouldquit'] == 'quit') {
            $quitter = true;
            continue;
        }
        //important connection stuff here...      
    }
 }


you can set lower max_execution_time in runtime:

ini_set('max_execution_time', 10); // maximal execution time in seconds

This ensures that the script itself ends after 10 seconds, even if it has not been completed. Time you have to choose, depending on how long it can take up.

0

精彩评论

暂无评论...
验证码 换一张
取 消