Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I wonder how can I make the machine do something each time it connects to internet ?

like checking for updates once I'm connected to internet ?

Thanks

share|improve this question
add comment

4 Answers

There is absolutely no indicator that shows whether you have an internet connection or not. That is because you always connect to some kind of gateway (router, modem, ...) that sets up the connection to your ISP. So, to be sure whether you are online or not, you have to check the connection yourself by pinging or building up an HTTP connection (using curl for example) and checking the result. But be warned: a ping response doesn't mean you can have an HTTP connection. And getting an HTTP response doesn't mean you can surf the web. You always have to check the response.

You can be sure to have an internet connection if you know the expected response. For example, you can parse websites like http://www.whatismyip.de/ for an IP address using regular expressions. If your regular expression is correct and it doesn't find an IP address then you have no internet connection.

Short answer: Write a script in which you check the internet connection (like described above). On success you execute the commands you want and set a flag that you are done for today (just create file and check for existance), on failure you do nothing. Make that script a cronjob.

share|improve this answer
    
Thanks dAnjou, But can you guide me how to start.. what are the basics.. I'm totally newbie in this !! Thanks again –  DeaDSouL Jun 22 '11 at 7:34
    
Well, one basic would be to learn shell scripting. There are tons of tutorials for that. I can't recommend a particular one. The next thing to read about would be cronjobs. See Davidc's answer. It is a good start but if you do it like that and set up a cronjob for doing it every 5 minutes, your system probably will be updated every 5 minutes. I don't think this is good. If you updated your system, I'd say you should create a file like this touch updated_at_$(date +%F) and check for it the next time before updating. –  dAnjou Jun 25 '11 at 13:55
add comment

The script you are looking for looks like this

[ -n "$(curl -s icanhazip.com | grep ^[0-9])" ] && firefox
  • "curl icanhazip.com" returns an IP adress if you are online
  • "grep ^[0-9]" checks if the first character is a digit
  • "[ -n "..." ] &&" means "if this is true, proceed to the next command (which in my example is "firefox")"
  • This is a quick hack and this script won't work if icanhazip.com is down or if you have an IPv6 adress.

To easily setup a cronjob to run it every x minutes, you can install "gnome-schedule".

share|improve this answer
add comment

Network-manager exposes a DBus interface that you can use to be notified when you connect to a network. This will usually mean you're connected to the internet as well, but I don't know if Network-Manager has any methods for actually confirming that. You might want to use Network-Manager to get notified when you connect to a network, then confirm that you have internet access and if so, do what you want.

share|improve this answer
add comment

As dAnjou said, I don't believe there is a trigger you can use.

However, to check for an Internet connection, there is an easier way than using curl. Use fping as follows, which returns 0 if connected and 2 if not. See fping's manual for more information.

fping -q example.com

(Using example.com is valid, because the domain name exists even though it is reserved for testing.)

Simplifying Davidc's example becomes:

fping -q example.com && firefox
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.