开发者

Is there a way to check if a host is up?

开发者 https://www.devze.com 2023-02-04 19:59 出处:网络
I\'m trying to do this in PHP. I need to check if a specified host is \"up\" I thought of pinging the specified host (though I\'m not sure how I would, since that woul开发者_开发问答d require root. -

I'm trying to do this in PHP. I need to check if a specified host is "up"

I thought of pinging the specified host (though I'm not sure how I would, since that woul开发者_开发问答d require root. --help here?)

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.

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.


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.


The short answer is that there is no good, universal way to do this. Ping is about as close as you can get (almost all hosts will respond to that), but as you observed, in PHP that usually requires root access to use the low port.

Does your host allow you to execute system calls, so you could run the ping command at the OS level and then parse the results? This is probably your best bet.

$result = exec("ping -c 2 google.com");

If a host is blocking a ping request, you could do a more general portscan to look for other open ports (but this is pretty rude, don't do it to hosts who haven't given you specific permission). Nmap is a good tool for doing this. It uses quite a few tricks to figure out if a host is up and what services may or may not be running. Be careful though, as some shared hosting providers will terminate your account for "hacking activity" if you install and use Nmap, especially against hosts you do not control or have permission to probe.

Beyond that, if you are on the same unswitched ethernet layer as another host (if you happen to be on the same open WiFi network, for example), an ethernet adaptor in promiscuous mode can sniff traffic to and from a host even if it does not respond directly to you.


You could use cURL

$url = 'yoururl';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode) {
    // All's well
} else {
    // not so much
}


For the host to be monitored at all, at least one port must be open. Is the host a web server? If so you could just open a connection to port 80, as long as it's opened successfully then at least some part of the host is working.

A better solution would be to have a script that is web accessible to just your monitor, and then you could open a connection to that, and that script would return various bits of system info.

EDIT--

How thorough do you want this test to be? [server on] -> [apache running] -> [web application working] Are all different levels of working. Just showing apache is returning something does at least show the server is on, but not that your web app is running. (I realise that you may not be running anything like this but I hope it's a useful example)

EDIT--

Would it be worth installing a lightweight http server (I mean very light weight) just for monitoring?

Failing that could you install something on the hosts that phoned home every so often to show they are up?


I used gethostbyname($hostname).

The function gives you the IP if the host is up, or the input hostname if it couldn't find the IP.

if ($hostname !== gethostbyname($hostname)) {
    //Host is up
}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号