检查服务器是否在线 [英] Check if a server is online

查看:91
本文介绍了检查服务器是否在线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在实施LDAP身份验证.如果LDAP服务器处于脱机状态,我会缓存凭据作为备用.作为此缓存的一部分,我需要检查LDAP服务器是否在线.与其使用PHP的Ldap方法,不如使用诸如ping之类的简单方法,会更好.

I'm currently implementing LDAP Authentication. I cache the credentials as a fallback in case the LDAP server is offline. As part of this caching I need to check if my LDAP server is online. Rather than using PHP's Ldap methods it would be better to use something simple like a ping.

请注意,它应该能够处理任何协议.例如,我不能使用 fsockopen ,因为它不支持 ldaps://.[我知道我可以注册自己的协议包装器.]

Please note that it should be able to handle any protocols. E.g., I can't use fsockopen because it does not support ldaps://. [I know that I could register my own protocol wrappers].

我希望此检查是通用且简单的.

I want this check to be generic and simple.

推荐答案

我正为此目的使用 fsockopen .我是否知道它是否支持 ldaps 都无关紧要,因为最后有两种可能性:

I'm using fsockopen for exactly that purpose. It doesn't matter whether it supports ldaps or not I figured out, because there are two possibilities in the end:

  • 适当的端口已打开,因此我可以假定LDAP服务器已启动并正在运行,或者
  • 适当的端口未打开 ,因此我可以假定LDAP服务器正在运行 not .
  • The appropriate port is open, so I can assume that the LDAP-Server is up and running or
  • The appropriate port is not open, so I can assume that the LDAP-Server is not running.

您可以像这样检查:

$fp = @fsockopen($host, $port, $errno, $errstr, 30);
if (! $fp) {
    // Port is unavailable
}
fclose($fp);

现在您知道,要连接的端口已打开,我可以启动LDAP.

Now you know, the port to connect to is open and I can fire up LDAP.

我发现了两个边缘情况,但是您将无法检查是否使用此方法

I've found two edge-cases that you won't be able to check for using this method though

  • LDAP服务器处于未定义状态,并且端口仍处于打开状态,但没有响应或
  • 其他一些应用程序已打开端口.

您可以使用

$con = ldap_connect($ldapURI);
if (! ldap_bind($con, $user, $password)) {
    // Something is fishy
}

Fishy可能是无效的凭据(在第一次绑定时不应该发生这种情况,对吗?),或者侦听该端口的服务器未按照我们期望的方式响应.因此它不是LDAP服务器,或者服务器处于未定义状态.

Fishy might be invalid credentials (which should not happen at this first bind, right?) or the server listening on that port is not responding in a manner that we expect. So it's either not an LDAP-Server or the server is in an undefined state.

要快速失败,您应该适当地调整超时时间,这样就不必等到半分钟就知道出了点问题.

To fail fast, you should adapt the timeouts appropriately so you're not waiting half a minute just to know that something went wrong.

您可以使用第五个参数设置 fsockopen 的超时时间,而您可以使用

YOu can set the timeout for fsockopen using the fifth parameter and you can set the timeouts for LDAP using

ldap_set_option($con, LDAP_OPT_NETWORK_TIMEOUT, [whatever is appropriate]);
ldap_set_option($con, LDAP_OPT_TIMEOUT, [whatever is appropriate]);
ldap_set_option($con, LDAP_OPT_TIMELIMIT, [whatever is appropriate]);
// Only available when your LDAP-extension is compiled against the Netscape LDAP C-SDK
ldap_set_option($con, LDAP_X_OPT_CONNECT_TIMEOUT, [whatever is appropriate]);

您需要在之后 ldap_connect 但在之前 ldap_bind 设置它们.

You'll need to set them after ldap_connect but before ldap_bind.

尽管尚未在php.net上记录LDAP_OPT_TIMEOUT和LDAP_X_OPT_CONNECT_TIMEOUT!

LDAP_OPT_TIMEOUT and LDAP_X_OPT_CONNECT_TIMEOUT are not (yet) documented on php.net though!

有关这些常量的更多信息,请参见 https://linux.die.net/man/3/ldap_set_option ,但请注意,并非其中提到的所有常量都在PHP-LDAP-Extension中实现.

For more infos on these constants have a look at https://linux.die.net/man/3/ldap_set_option but beware that not all the constants mentioned there are implemented in the PHP-LDAP-Extension.

这篇关于检查服务器是否在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆