sh code to determine if host is on lan

Polytropon freebsd at edvax.de
Sat Dec 8 02:38:52 UTC 2018


On Fri, 07 Dec 2018 17:07:19 -0500, Ernie Luzar wrote:
> Doug McIntyre wrote:
> > On Thu, Dec 06, 2018 at 07:53:37PM -0500, Ernie Luzar wrote:
> >> 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.
> > 
> > Hmm, I thought my host here was on a LAN..
> > 
> > vmx0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
> >      inet 216.250.176.100 netmask 0xffffffe0 broadcast 216.250.176.127
> >      inet6 2001:4980:2:dad::100 prefixlen 64
> > 
> > And this one too..
> > bce0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
> >      inet6 2001:4980:0:1000:21e:c9ff:feb5:663a prefixlen 64 autoconf
> >      inet6 2001:4980:0:ffff:21e:c9ff:feb5:663a prefixlen 64 autoconf
> > 
> > Are you sure your definition of "LAN" is the correct term?
> > 
> > What is it you are trying to determine?
> > 
> > 
> > 
> Like most ISPs my ISP doesn't provide ipv6 service. So not interested in 
> it until it goes public nationwide.
> 
> Your 216.250.176.100 ipv4 address is public routeable. Looks like this 
> machine is running vm and the freebsd os is configured as the gateway to 
> the public internet. If it has a LAN behind it using ipv4 addresses, 
> then this gateway is also running a firewall with NAT so the LAN nodes 
> can reach the public internet.
> 
> I'm playing with 12.0-RC3 and vnet jails. Vnet jails come in two flavors 
> based on the hosts network topography. The host is either the gateway or 
> its a node on the lan. Lans use these reserved non-routeable ipv4 
> address ranges for network communication. If the host's default ip 
> address is in the range covered by one of theses 192.168/16 or 172.16/12 
> or 10/8 ranges then the host is a node on a lan. That is what I am 
> trying to determine in a sh script. I know that I can drill down through 
> a few standard commands to capture the hosts external ipv4 address easy 
> enough. But comparing it to the different ranges becomes a painful 
> coding task.

So I understood your intention correctly: "Public or private IP?"
is the question you've been trying to answer.



> I think I just figured out the simple method I was looking for. Just 
> issue the whois command using the captured hosts external ipv4 address 
> and interrogate the reply for "Private Use".

That is an interesting method!

% whois 192.168.45.67 | grep "NetName:"
NetName:        PRIVATE-ADDRESS-CBLK-RFC1918-IANA-RESERVED

% whois 172.16.50.10 | grep "NetName:"
NetName:        PRIVATE-ADDRESS-BBLK-RFC1918-IANA-RESERVED

% whois 10.232.3.64 | grep "NetName:"
NetName:        PRIVATE-ADDRESS-ABLK-RFC1918-IANA-RESERVED

The surrounding grepping, cutting, and $? magic is trivial. :-)


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


More information about the freebsd-questions mailing list