17

I want to see if an Amazon EC2 instance is open, but by default Amazon EC2 blocks ping requests. There is a way to do this by changing the security policy of the instance. Excluding that, what would be the closest alternative to "see if a server is responsive" and is light-weight?

Is curl a good option?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David542
  • 104,438
  • 178
  • 489
  • 842
  • If you arrive here from google try using a bash socket to open a port https://stackoverflow.com/a/4743398/619760 – nelaaro Mar 26 '21 at 08:03

3 Answers3

18

I think curl is exactly what you want. Like all well-behaved unix programs, it returns an error code if anything goes wrong:

[cnicutar@ariel ~]$ curl www.no-such-website.com
[cnicutar@ariel ~]$ echo $?
6

Also you may want to use --connect-timeout to make sure it doesn't wait forever.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Thank you, I will use that. Out of curiosity, what does `6` mean as an error code? – David542 Apr 11 '13 at 22:00
  • @David542 It's `curl`-specific. Every program chooses what to return and there's little standardization. If you're really interested you can look for a header called `sysexits.h`. The manual page does however explain exit codes. – cnicutar Apr 11 '13 at 22:01
  • from `curl` manpage: `6 Couldn't resolve host. The given remote host was not resolved.` – dschulz Apr 11 '13 at 22:21
  • @dschulz So that's that. Everything not 0 is an error, which is what the OP is probably after. – cnicutar Apr 11 '13 at 22:22
  • You're right @cnicutar. I was just too curious about the 6 too and went to try `man curl` then `/EXIT CODES` :-) – dschulz Apr 11 '13 at 22:24
  • @dschulz I too looked earlier but didn't find it straight away and weaseled out: "I'm sure it's there". – cnicutar Apr 11 '13 at 22:28
5

CURL won't work as a quick check if the server is up and the web server daemon is down. One alternative is to send a TCP ACK with tools like hping3. If you get a RST, the server is UP.

hping3 -c 1 -V -p 80 -s 5050 -A example.fqdn

What you could do is try the CURL test first and on failure try the second method to confirm that the server is down and not just the web server daemon.

The problem with this method is that a stray TCP packet like this could be filtered by an intermediate proxy.

jman
  • 11,334
  • 5
  • 39
  • 61
  • Sure it will work. If the web server daemon is down the box will reply with a TCP RST or with nothing at all (which will be caught by the timeout). At any rate, if there's no response in the specified time `curl` will know. – cnicutar Apr 11 '13 at 22:25
  • Okay, thats true. Its so much faster to get a RST with hping3 than to wait for the exponential SYN retransmit back-off to timeout. Compare curl with the hping test for various tests (non-existent host, down host, down port, etc.). Its generally quicker. – jman Apr 11 '13 at 22:30
5

If you can ssh to it then it's up. A simple way of checking this with nc is: nc -zv 111.222.333.444 22 (Replace 111.222.333.444 with the ip address of your instance)

msa
  • 702
  • 7
  • 14
Lars Hansson
  • 366
  • 1
  • 3
  • Thanks, what is the difference between netcat and ping? Also, the above command does not work in its current format. – David542 Apr 12 '13 at 18:19
  • The command works fine, I tested it before I posted. What error do you get? – Lars Hansson Apr 13 '13 at 03:07
  • @LarsHansson from docker container, I have to do nc mysql in my local, do i need to replace port in your example to 3306? – pm1359 Apr 28 '21 at 16:06