38342

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

Question:

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 would 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.

Answer1:

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.

Answer2:

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). <a href="http://nmap.org/" rel="nofollow">Nmap</a> 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.

Answer3:

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 }

Answer4:

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?

Recommend

  • How to get the relative position of a Dictionary element?
  • Problem with sockets in C#
  • Condor job using DAG with some jobs needing to run the same host
  • Index.php as custom error page
  • C++ accessing vector
  • Iterate twice through a DataReader
  • Execute scripts AJAX returns
  • Is playing sound in Javascript performance heavy?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Insert into database using onclick function
  • Master page gives error
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Knitr HTML Loop - Some HTML output, some R output
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Redux, normalised entities and lodash merge
  • Apache 2.4 - remove | delete | uninstall
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • php design question - will a Helper help here?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Cannot Parse HTML Data Using Android / JSOUP
  • PHP: When would you need the self:: keyword?
  • Android Studio and gradle
  • AngularJs get employee from factory
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • How to get NHibernate ISession to cache entity not retrieved by primary key