You are not logged in.
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
i know that is not the best way, but it works
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
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
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
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
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
I use ping:
if ping -c1 www.google.com; then
Do connected stuff
else
Do disconnected stuff
fi
Offline
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