判断服务器是否在线

bandit 发布于 2024-10-03 447 次阅读


原理:

我们可以通过在服务器上一直运行的服务来判断服务器是否正常在线(比如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;
    }
}
此作者没有提供个人介绍。
最后更新于 2024-10-03