I'd like to be able to test if I have access to the internet. My computer is connected to the residence internet connection. I thought of using ping like this:

ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error

But this does not work!

As I said the connection is provided by a residence(which is actually just forwarding the university connection). To have access to the internet you must open a web browser where you are requested user name and password and only then the internet access is granted.(I really have no clue about networks configurations etc. and anyway I do not have any access to how it is setup, so I can't provide more information on this side)

The above code prints "ok" even when I do not actually have access to the internet. Even testing with www.google.com fails. I believe this is because it is being redirected to the login page and it "thinks" to get back the packets correctly.

How can I reliably check if I have access to the internet?

Some context

I'd like to connect to the computer in my room via ssh when I go home during weekends. The connection sometimes fails so I'd like to create a script that every once in a while checks for the connection and eventually starts a web browser and sends keystrokes using xdotool to login. If you know a better way to reopen the connection to allow ssh remote access than, please, write it too.


Some sample output of nslookup:

$ nslookup unix.stackexchange.com

Non-authoritative answer:
Name:   unix.stackexchange.com

The result is exactly the same both with internet access and without it. (I've also tried to log off and relog, and connect and disconnect multiple times).

Also, I'd like to add that is not the ssh connection closing. For some time I'm able to create the connection, do things and log out. After some hours trying to do ssh the-IP gives a timeout error(I actually registered a no-ip account since the IP is dynamic so I'm doing ssh mydomain.no-ip.org).

  • Try querying the DNS servers (if it is Internet based): nslookup unix.stackexchange.com. If you get a full answer, then you have internet connection. Mind you, quite a few firewalls only block TCP traffic and forget all about UDP. In that case DNS-queries are resolved properly while you will not be able to download any content from a webserver. – jippie Nov 18 '12 at 21:18
  • @jippie I tried now but it does not seem to work, it returns the same output when having internet access and when not having it. Maybe the DNS lookup is cached? If so how to clear the cache? – Bakuriu Nov 18 '12 at 21:23
  • On a side note, during this weekend I tested that ssh works from home, but after some hours the connection was lost. – Bakuriu Nov 18 '12 at 21:31
  • Totally depends on what DNS server you're querying. That is in the answer being returned (Server: ...). If it is a public IP, then you have internet connection, if it is a private IP address then you just won't know (unless you can think of a hostname that nobody else thought of for the past 24hrs or so. – jippie Nov 18 '12 at 21:42
  • SSH can time out on its own when the SSH tunnel is idle. There can be many reasons that causes this behaviour (including a shared firewall). – jippie Nov 18 '12 at 21:44

You made a fatal mistake in your command.

ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error

The command ip r | grep default | cut -d ' ' -f 3 returns your default gateway. As a result, ping, instead of checking Google, checks the router you are connected to. The router will send back a packet, which makes the whole command true. That's why it always returns "ok". It does not check internet connectivity at all. But, look at the bright side. It checks if you are connected to LAN.

Anyways, you may want to use ping -q -w 1 -c 1 google.com > /dev/null && echo ok || echo error instead of using ip r | grep default | cut -d ' ' -f 3.

| improve this answer | |
  • 1
    Did I just bump an old thread? sudden realization – PNDA Mar 17 '15 at 9:16
  • Yes, although I still find your answer useful. Thanks! – Bakuriu Mar 17 '15 at 14:27

Sounds like you need a program like AutoWifi.

| improve this answer | |
  • Yes, but I have to connect through an Ethernet cable and not wifi. – Bakuriu Nov 19 '12 at 6:35

How about IF?

if [[ $(ping -c1 -w1 google.com) ]]
    echo "All good in the hood"
    echo "Where are my interwebs?"

or as one line

if [[ $(ping -c1 -w1 google.com) ]]; then  echo "All good in the hood"; else; echo "Where are my interwebs?"; fi
| improve this answer | |
  • This does not work. It will always return "true" even when I don't actually have internet access. – Bakuriu Nov 19 '12 at 8:32

The only way to do what I required seems to download a random webpage and see if the contents match the login page or not.

For example doing:

$ wget www.google.com

Will download the html content of google's page if I have internet access, while it would just contain something like:

**  ZEROSHELL Captive Portal Login Page  
**          (Native Interface)

<title>Web Login</title>

If it's being redirected to the login page. It seems to work well to detect what I want, even though I hoped to find a more elegant solution.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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