1

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.

Update:

Some sample output of nslookup:

$ nslookup unix.stackexchange.com
Server:         127.0.1.1
Address:        127.0.1.1#53

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

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).

12
  • 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. Commented Nov 18, 2012 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? Commented Nov 18, 2012 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. Commented Nov 18, 2012 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. Commented Nov 18, 2012 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). Commented Nov 18, 2012 at 21:44

4 Answers 4

2

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.

2
  • 1
    Did I just bump an old thread? sudden realization Commented Mar 17, 2015 at 9:16
  • Yes, although I still find your answer useful. Thanks! Commented Mar 17, 2015 at 14:27
0

Sounds like you need a program like AutoWifi.

1
  • Yes, but I have to connect through an Ethernet cable and not wifi. Commented Nov 19, 2012 at 6:35
0

How about IF?

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

or as one line

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

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)
**
-->

<html>
<head>
<title>Web Login</title>
    ...
</html>

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.