views:

342

answers:

3

How would I check if the remote host is up without having a port number? Is there any other way I could check other then using regular ping.

There is a possibility that the remote host might drop ping packets

A: 

The best you can do is:

  • Try and connect on a known port (eg port 80 or 443 for HTTP or HTTPS); or
  • Ping the site. See Ping a site in Python?

Many sites block ICMP (the portocol used to ping sites) so you must know beforehand if the host in question has it enabled or not.

Connecting to a port tells you mixed information. It really depends on what you want to know. A port might be open but the site is effectively hung so you may get a false positive. A more stringent approach might involve using a HTTP library to execute a Web request against a site and see if you get back a response.

It really all depends on what you need to know.

cletus
A: 

Many firewalls are configured to drop ping packets without responding. In addition, some network adapters will respond to ICMP ping requests without input from the operating system network stack, which means the operating system might be down, but the host still responds to pings (usually you'll notice if you reboot the server, say, it'll start responding to pings some time before the OS actually comes up and other services start up).

The only way to be certain that a host is up is to actually try to connect to it via some well-known port (e.g. web server port 80).

Why do you need to know if the host is "up", maybe there's a better way to do it.

Dean Harding
A: 

A protocol-level PING is best, i.e., connecting to the server and interacting with it in a way that doesn't do real work. That's because it is the only real way to be sure that the service is up. An ICMP ECHO (a.k.a. ping) would only tell you that the other end's network interface is up, and even then might be blocked; FWIW, I have seen machines where all user processes were bricked but which could still be pinged. In these days of application servers, even getting a network connection might not be enough; what if the hosted app is down or otherwise non-functional? As I said, talking sweet-nothings to the actual service that you are interested in is the best, surest approach.

Donal Fellows