sh code to determine if host is on lan

Ernie Luzar luzar722 at gmail.com
Fri Dec 7 00:53:41 UTC 2018


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

Connected to the internet is NOT the question.

Is this host a node on the lan is the question.

192.168/16 or 172.16/12 or 10/8, these ip address ranges are 
non-routeable over the public internet. By standards they are reserved 
for lan addresses. If the host being inspected has been assigned one of 
these ip address would indicate its a node on the lan.

Using a sh script is there a simpler way of finding this out without 
drilling down through those commands I posted above?

What I am hearing so far is no there is short cut method.

Thanks for your replies.









More information about the freebsd-questions mailing list