PHP ssh2_connect() Implement A Timeout

时光毁灭记忆、已成空白 提交于 2019-12-21 10:49:40

问题


I am using the PHP ssh2 library, and simply doing:

$ssh = ssh2_connect($hostname, $port);

The problem is I want to set a timeout, i.e. after 5 seconds stop trying to connect. As far as I can tell the ssh2 library does not support a timeout on connect natively. How can I implement a timeout wrapper?


回答1:


I know this is an old thread, but the problem is still there. So, here is the solution for it.

ssh2_connect() uses socket_connect(). socket_connect relies on the php ini configuration parameter default_socket_timeout which is set by default to 60 seconds (http://php.net/manual/en/filesystem.configuration.php#ini.default-socket-timeout)

So, the easiest way to solve our problem is to change the ini setting at runtime to a value we want, and than back to the value set in the ini file so we avoid effecting other parts of our software. In the example below, the new values is set to 2 seconds.

    $originalConnectionTimeout = ini_get('default_socket_timeout');
    ini_set('default_socket_timeout', 2);

    $connection = ssh2_connect('1.1.1.1');

    ini_set('default_socket_timeout', $originalConnectionTimeout);

You can find further details about how ssh2 for php works by reading the source code of libssh2 (https://github.com/libssh2/libssh2).




回答2:


I've been struggling with the same problem for a while. The fact is that trying to connect to a "dead" or unresponsive server with ssh2 will stall your application for as long as the target server's max connection time limit.

 

The easy way to detect beforehand if your instance is going to cause you trouble when shh-ing into it is to ping it (see if it's responsive).

function getPing($addr) 
{
    //First try a direct ping
    $exec = exec("ping -c 3 -s 64 -t 64 ".$addr);
    $array = explode("/", end(explode("=", $exec )) );
    if(isset($pingVal[1]))
    {
        //There was a succesful ping response.
        $pingVal = ceil($array[1]);
    }
    else
    {
        //A ping could not be sent, maybe the server is blocking them, we'll try a generic request.
        $pingVal = callTarget($addr);
    }


    echo intval($pingVal)."ms";
    if($pingVal > ["threshold"])
    {
        return false;
    }
    return true;
}

function callTarget($target)
{
    $before = microtime();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    if (curl_exec($ch))
    {
        curl_close($ch);
        return (microtime()-$before)*1000;
    }
    else
    {
        curl_close($ch);
        return 9999;
    }
}

This method allows you to get a faster response on the state of your server, so you know if you are about to waste your time ssh-ing into it.




回答3:


the libssh2 library doesn't do the connect() by itself so it doesn't have to provide a timeout for that. libssh2 does however offer timeouts for the functions it does provide...




回答4:


Using ping execution may cause problem if firewall on remote side block ping packets. Also this may cause problems on different environments or may be there other obstructions as acces restrictions about executing commands.

You can use ini_get, ini_set aprroach for 'default_socket_timeout' as previously well metioned by Patkos Csaba. But from my point of view is better to use test socket connection with specified timeout:

function sock_online_test($ip, $timeout=0.1, $port='22') {
    $fp = @fsockopen($ip, $port, $errno, $errstr, $timeout);
    if (!$fp) {
        return false;
    } else {
        fclose($fp);
        return true;
    }
}

$online = sock_online_test('192.168.1.1', 5);
// even domain may works
$online = sock_online_test('myserver.com', 5);


来源:https://stackoverflow.com/questions/10730556/php-ssh2-connect-implement-a-timeout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!