有没有办法检查主机是否启动? [英] Is there a way to check if a host is up?
问题描述
我正在尝试用PHP做到这一点.我需要检查指定主机是否启动"
I'm trying to do this in PHP. I need to check if a specified host is "up"
我考虑过对指定的主机执行ping操作(尽管我不确定如何执行此操作,因为这将需要root用户.--help此处?)
I thought of pinging the specified host (though I'm not sure how I would, since that would require root. --help here?)
虽然我也使用fsockopen()
尝试在指定的端口上进行连接,但是如果主机未在该端口上侦听连接,那也将失败.
I also though of using fsockopen()
to try to connect on a specified port, but that would fail too, if the host wasn't listening for connections on that port.
另外,一些主机会阻止ping请求,那么我该如何解决呢?不过,这部分不是必需的,因此不必为此担心太多.我意识到这一点可能会很棘手.
Additionally, some hosts block ping requests, so how might I get around this? This part isn't a necessity, though, so don't worry about this too much. I realize this one might get tricky.
推荐答案
我通常为公共页面做一个简单的cURL,看看它是否返回200.如果得到500、404或除200响应之外的任何内容,知道有什么鱼腥了.
I typically do a simple cURL for a public page and see if it returns a 200. If you get a 500, 404, or anything besides a 200 response you know something fishy is up.
这篇关于有没有办法检查主机是否启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!