You are not logged in.

#1 2009-11-06 11:27:47

fiod
Member
Registered: 2007-04-02
Posts: 205

How to know if the computer is connected to the net?

Hi,

Is there some simple way to know that (using bash scripting or whatever)?
Besides simply pinging some site, and doing some text manipulation to understand that.
Something in /proc/net maybe?

Thanks
fiod

Offline

#2 2009-11-06 11:44:05

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: How to know if the computer is connected to the net?

i know that is not the best way, but it works tongue
beside im not that good at bash scripting...

  1 #!/bin/bash
  2 
  3 server=www.google.com
  4 
  5 if [ -z `ping $server -c 4 | grep "100% packet loss"` 2> /dev/null ]
  6         then
  7                 echo "$server OK!"
  8         else
  9                 echo "$server Unavailable!"
 10         fi

Offline

#3 2009-11-06 12:15:50

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: How to know if the computer is connected to the net?

I use the following in /etc/rc.local to fallback to my wireless interface where necessary:

if ! /usr/sbin/ip address show eth0 | /bin/grep -E "^[\t ]*inet" &> /dev/null; then
    /usr/bin/netcfg wlan0 &
fi

Basically, eth0 uses DHCP to aquire an IPv4 address.  If it hasn't been assigned an address (i.e. couldn't contact a DHCP server), then I assume it to be unplugged and attempt to fall back to wlan0.  Also worth investigating is ifplugd, which checks network interfaces for a heartbeat.

However, these all relate to the local network.  If you're referring to the internet, you'll need to test that you can send traffic to a remote host -- there's really no way around it.

Last edited by chpln (2009-11-06 12:18:05)

Offline

#4 2009-11-06 16:47:27

lilsirecho
Veteran
Registered: 2003-10-24
Posts: 5,000

Re: How to know if the computer is connected to the net?

pacman -Syu


Prediction...This year will be a very odd year!
Hard work does not kill people but why risk it: Charlie Mccarthy
A man is not complete until he is married..then..he is finished.
When ALL is lost, what can be found? Even bytes get lonely for a little bit!     X-ray confirms Iam spineless!

Offline

#5 2009-11-06 20:51:18

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: How to know if the computer is connected to the net?

A good question:  How connected do you have to be before you are considered "connected to the Internet"?  Is connected to a local intranet sufficient?  Any kind of wired or wireless connection?  At my workplace, I have a connection most would consider to be a full Internet connection, but there are some sites (proxies, porn sites, and some miscategorized file sharing sites) that I can't connect to.  Am I "connected to the Internet" for the purposes of your script?

If this is part of a larger program that depends on a connection to specific sites, it's better to test the connection to the sites you need rather than try to abstractly define "connected to the Internet".  For general "am-I-online-or-not" use like you might want to put in a notification area, I would go ahead and just ping Google -- it works probably 99.9999% of the time (unless pings are filtered, in which case you could send a GET request) and doesn't incur much slowdown.

Offline

#6 2009-11-06 22:00:11

fiod
Member
Registered: 2007-04-02
Posts: 205

Re: How to know if the computer is connected to the net?

Thanks for all replies.

Trent: Good question.
I think that local intranet is OK.

What about looking at /sys/class/net/eth1/operstate?

What do you think?

Offline

#7 2009-11-06 22:00:51

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: How to know if the computer is connected to the net?

I use ping:

if ping -c1 www.google.com; then
    Do connected stuff
else
    Do disconnected stuff
fi

Offline

#8 2009-11-11 22:32:11

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: How to know if the computer is connected to the net?

arp -a will give you your router's IP and MAC address, and which interface it's connected via. It can be used to work out which network you're connected via. Eg my outbound SMTP server changes depending on whether I'm connected via my ISP, or via work, or a public network. So I have set-up a script that uses arp to change it.

The only problem is that arp -a sometimes returns nothing even when connected, for the first x minutes of connection. I haven't yet worked out why, and it's probably something to do with my system, rather than a general arp issue...I think.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB