You are not logged in.
Hi all,
how can I know in a shell script if my laptop has a network connection up and running, is in a lan (and in this case which one?) and/or it can reach internet or if the ethernet cable is not plugged in? Where can such info be found?
I actually use at least 3 different network profiles. I don't need wireless lan ( yet ).
Something like
IPADDR=`ifconfig | grep 'Bcast'| awk '{print $2}' | sed s/addr://`
GATEWAY=`route -n | grep -e '^0' | awk '{print $2}'`
and then manually pinging the gateway is the only solution?
domanov
Offline
ifconfig is all I ever use. That and the very occasional ping to the dns address in the event I question if it's up or not. I usually ping the dns instead of the gateway for the obvious reason that the dns won't return unless I'm actually on the net and not just on my local network.
Last edited by Acid7711 (2007-06-26 16:09:35)
Offline
the script pings google and if there's no connection it prints ISP MUSTDIE, just replace the echo command with any command u need:
#! /bin/bash
res=`ping google.com -c 1 -q -W 2 -w 2 | grep '1 packets transmitted, 1 received, 0% packet loss' | wc -l`
#echo ">>> $res"
if [ "$res" -eq "1" ]
then echo OK
else echo "ISP MUSTDIE"
fi
Offline
nice, thanks
Offline
Thank you for the nice answers, maybe my question was not so clear.
I meant: is this (ifconfig+ping) the only way? The answer seems to be yes, though
Other question: how can I retrieve which network profile was chosen at startup (with 'menu' in rc.conf)? The aim is to properly set env.var. such as http_proxy which depend on the profile. Or can this simple task get done directly in the netprofile scripts?
Offline