Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my bash scripts I have been using something like:

until fping -u; do echo "$0[$$] Network/DNS down?? $(date)" 1>&2 && sleep $(($RANDOM%(1 + ++trynum * 1) +1)).222; done

to test for online connectivity.

It halts in place, sleeping growing random intervals, until it can ping again.

Problem: On some LANs ICMP pings are blocked altogether, although web pages are still reachable. What's a short way to test for this general case (firewall to outside)?

Based on that test I will switch over to an http-based test like the exit status of

curl -s >/dev/null

if that is a good one.

share|improve this question

Using curl is rather wasteful of bandwidth. Just use tcping instead.

Other options if you don't have that:

curl -sI -m 2 >/dev/null
echo 'HEAD / HTTP/1.0\r\nHost:\r\n\r\n' | nc 80 >/dev/null

The basic idea is to make a TCP connection to port 80 and see if that succeeds.

share|improve this answer
Good tool, but doesn't seem universal. I would have to deploy on each Ubuntu/cygwin server I have control over. Looking for a more portable way. – Marcos Jun 13 '12 at 11:30
@marcos What do you have? zsh? netcat? – Keith Jun 13 '12 at 11:38
on most Ubuntu hosts: bash, dash, ash, socat, netcat/nc, nmap, fping, arping, mtr, traceroute, netstat, netpipes(faucet hose), curl, wget, no zsh ... – Marcos Jun 13 '12 at 11:50

If some server is blocking ICMP requests, you can still check if the web server is reachable by a short port scan e.g. with nmap:

nmap -PN -T4 -p 80 your_webserver 2>&1 | grep -wi open >/dev/null || date

share|improve this answer
Okay, except nmap -PN -T4 -p 80 || date doesn't execute date as desired; apparently the exit code is still 0 when hostname can't resolve (or reachable?) – Marcos Jun 13 '12 at 11:44
@Marcos: Add grep "open". – speakr Jun 13 '12 at 11:50
true. nmap -PN -T4 -p 80 |grep -wi open || failcode – Marcos Jun 13 '12 at 11:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.