原理:
我们可以通过在服务器上一直运行的服务来判断服务器是否正常在线(比如SSH)。
实现(PHP80 SSH2):
//需要安装PHP SSH2扩展!
function is_online($host = "localhost", $port = 22, $username = "root", $password = "", $timeout = 2) : bool
{
try
{
$ssh = @ssh2_connect($host, $port, ['timeout' => $timeout]);
if($ssh === false) return false;
return @ssh2_auth_password($ssh, $username, $password);
}
catch (Exception $e)
{
return false;
}
}
