Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Dec 2018 01:19:05 +0100
From:      Polytropon <>
To:        JD <>
Subject:   Re: sh code to determine if host is on lan
Message-ID:  <>
In-Reply-To: <>
References:  <> <>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
elOn Thu, 06 Dec 2018 16:06:35 -0700, JD wrote:
> On 12/06/2018 03:14 PM, Ernie Luzar wrote:
> > Hello list
> >
> > Know that "route -n get default" will give me the nic name of the 
> > interface connected upstream. That "ifconfig nic" will give me the ip 
> > address. That if that ip address is one of these ranges
> > 192.168/16 or 172.16/12 or 10/8 then the host is on a lan.
> >
> > Is this the only way to determine if the host is on a lan?
> >
> I you do not want to know all that info you already
> stated, why don;t you ping something with a count of 1,
> such as
> ping -c 1
> [ $? -ne 0 ] && echo "not on internet" || echo "On Internet"

I think this doesn't answer the initial question (which I'm
not sure I have understood correctly); the question is: Is
the system on a LAN ("networked, but not on the Internet")?
Of course to check _if_ it is somehow connected to the Internet,
a simple ping is sufficient, as long as you can make sure
an ICMP echo request and its reply is not blocked somewhere
within the firewall; it's also imporatant to use good values
for tumeout and wait reply time - just because something _does_
answer a request too slow does not imply it cannot be reached.

For example, I use something like this to check if a set of
hosts is currently up:

	# ping timeout (in seconds) = ping -t
	# time to wait for ICMP echo reply packets (in milliseconds) = ping -W
	# ...
	ping -q -t ${TIMEOUT} -W ${WAITREPLY} -c 1 ${HOST} > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	# ...

I use this for a status display and SSH connection selector.
Note that there is still a difference between "is up" and
"will accept a SSH connection". ;-)

Similarly, you could use fetch (provided by OS), wget or
curl (additional ports) to get (and discard) a web page
you know is somewhere on the Internet. If your firewall
does only allow HTTP traffic, this would be a usable

The route | grep | ifconfig | grep approach initially shown
does obtain lots of information, discards most of them, but
of course answers the question if the system is currently
configured for LAN. However, with _multiple_ interfaces,
for example one for LAN, one for Internet, things get much
more interesting. :-)

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

Want to link to this message? Use this URL: <>