Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Dec 2018 01:19:05 +0100
From:      Polytropon <freebsd@edvax.de>
To:        JD <jd1008@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: sh code to determine if host is on lan
Message-ID:  <20181207011905.af7d5c29.freebsd@edvax.de>
In-Reply-To: <5C09AB7B.4010001@gmail.com>
References:  <5C099F41.2020407@gmail.com> <5C09AB7B.4010001@gmail.com>

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 yahoo.com:
> ping -c 1 yahoo.com
> [ $? -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
	TIMEOUT=2
	# time to wait for ICMP echo reply packets (in milliseconds) = ping -W
	WAITREPLY=100
	# ...
	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
solution.

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



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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20181207011905.af7d5c29.freebsd>