I have 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 stop your application until the maximum connection time for the target server is maximum.
An easy way to detect in advance if your instance will cause you problems when you log in to check it (see if it responds).
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 quickly respond to the status of your server, so you know whether you are going to spend your time to save time.
source share