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

I'm trying to write a Bash script that is aware of internet connectivity.

How can I achieve that?

For example:

if (something that checks internet connection);then
curl -o new someurl
exit 1
share|improve this question
up vote 3 down vote accepted

Try using the curl command and checking the exit status in an if statement.

curl -D- -o /dev/null -s
if [[ $? == 0 ]]; then
  exit 1
share|improve this answer

I was using snmp when I had a router that support it to get the ip assigned to the Wan interface (.253 was the router addr):

IP=$(/usr/bin/snmpwalk -v2c -Ov -cpublic \
          IP-MIB::ipAdEntAddr 2>/dev/null \
          | /bin/grep -v "\|\|")

Or maybe you could webscarp it from the router's stat pages with curl.

Currently instead I use to check/get the ip from the service I use for the dynamic dns:

IP=$(curl -s '' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g')

and then:

if [ "$IP" != "" ] && [ "$IP" != "" ]; then
   # connected
share|improve this answer

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.