Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
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
show 7 more comments

3 Answers 3

Sounds like you need a program like AutoWifi.

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

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
share|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
add comment
up vote 0 down vote accepted

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.

share|improve this answer
add comment

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.