Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In my bash scripts I have been using something like:

until fping -u google.com; 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 google.com 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 google.com >/dev/null

if that is a good one.

share|improve this question

2 Answers 2

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

Other options if you don't have that:

curl -sI -m 2  http://www.google.com >/dev/null
echo 'HEAD / HTTP/1.0\r\nHost: www.google.com\r\n\r\n' | nc www.google.com 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 sgasdfoogle.com || 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 sgasdfoogle.com |grep -wi open || failcode –  Marcos Jun 13 '12 at 11:51

Your Answer

 
discard

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.