You are not logged in.

#1 2007-06-26 11:55:09

domanov
Member
From: Italy
Registered: 2006-11-10
Posts: 45

Check if network is up and running

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

#2 2007-06-26 16:08:04

Acid7711
Member
From: Chicago, IL
Registered: 2006-08-18
Posts: 300
Website

Re: Check if network is up and running

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

#3 2007-06-26 21:07:52

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: Check if network is up and running

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

#4 2007-06-27 09:25:55

drakosha
Member
Registered: 2006-01-03
Posts: 253
Website

Re: Check if network is up and running

nice, thanks

Offline

#5 2007-06-30 15:53:01

domanov
Member
From: Italy
Registered: 2006-11-10
Posts: 45

Re: Check if network is up and running

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 wink

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

Board footer

Powered by FluxBB