Discussion:
Wrestling with IP addresses
Dave Hodgkinson
2018-06-13 10:57:05 UTC
Permalink
Please help a chap out.

I’ve been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP, Net::IP::Util and I’ve not been able to wrestle them into doing what I want.

Basically, if I have an address of 192.168.1.65 with a gateway of 192.168.1.254 I’d like to know if they’re on the same subnet. Same with 172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from the rfcs? Should I just check the first two bytes are the same? Wrong but trivial!

Ta,
Roger Bell_West
2018-06-13 11:14:31 UTC
Permalink
Basically, if I have an address of 192.168.1.65 with a gateway of 192.168.1.254 I’d like to know if they’re on the same subnet. Same with 172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from the rfcs? Should I just check the first two bytes are the same? Wrong but trivial!
Nope. Back in the days of class A/B/C you could just about do this, but
now we have CIDR and you can't. It's entirely legitimate to break up
192.168.0.0/16 into e.g. 192.168.0.0/24 and 192.168.1.0/24 and so on.

You need a netmask.
Julien Fiegehenn
2018-06-13 11:15:08 UTC
Permalink
Hello Dave,

you can do that with NetAddr::IP. Take a look at
https://stackoverflow.com/a/964212/1331451. You do need to put the mask in
the addresses though, as with IPs only it will assume /32.

This is from that answer.

use strict;
use warnings;
use NetAddr::IP;

my $first = NetAddr::IP->new('192.168.1.65/24');
my $second = NetAddr::IP->new('192.168.1.254/24');

if ($second->within($first)) {
printf "%s is within %s\n", $second, $first;
} else {
printf "%s is not within %s\n", $second, $first;
}


Cheers,

Julien
Post by Dave Hodgkinson
Please help a chap out.
I’ve been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP,
Net::IP::Util and I’ve not been able to wrestle them into doing what I want.
Basically, if I have an address of 192.168.1.65 with a gateway of
192.168.1.254 I’d like to know if they’re on the same subnet. Same with
172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from
the rfcs? Should I just check the first two bytes are the same? Wrong but
trivial!
Ta,
Simon Wilcox
2018-06-13 11:23:18 UTC
Permalink
Probably teaching you to suck eggs here but without knowing the netmask
of each address you cannot know for certain.

You could make a guess by finding the longest matching bit pattern from
the most significant end which will give you the largest possible subnet
but without the netmask you can't know if it's been further subdivided.

172.16.10.65 10101100.00010000 .00001010.01000001

172.16.254.254 10101100.00010000 .11111110.11111110

So if the netmask is /16 or less they're in the same subnet, but if it's
/17 or higher then they're not.

You could rely on A/B/C class defaults to make a guess but it's
extremely common to subdivide them (or indeed aggregate them) so you
can't know.

For instance 194.106.223.214 is a /24 by its class but is actually in a
/23 subnet.

Does that help ?

S.
Post by Dave Hodgkinson
Please help a chap out.
I've been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP, Net::IP::Util and I've not been able to wrestle them into doing what I want.
Basically, if I have an address of 192.168.1.65 with a gateway of 192.168.1.254 I'd like to know if they're on the same subnet. Same with 172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from the rfcs? Should I just check the first two bytes are the same? Wrong but trivial!
Ta,
Sue Spence
2018-06-13 11:33:14 UTC
Permalink
Another pre-employment screening, I would guess. This is bit like swallows
returning for summer. 🐊 🐊 (not swallows, but cute)
Post by Dave Hodgkinson
Please help a chap out.
I’ve been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP,
Net::IP::Util and I’ve not been able to wrestle them into doing what I want.
Basically, if I have an address of 192.168.1.65 with a gateway of
192.168.1.254 I’d like to know if they’re on the same subnet. Same with
172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from
the rfcs? Should I just check the first two bytes are the same? Wrong but
trivial!
Ta,
Paul Weaver
2018-06-13 11:49:19 UTC
Permalink
Surely the easiest way to check if a device is on the same network as another device is to issue an arp request for the target IP? I.E. send a ping, then check the arp table.

If you get a response, it's on the same network (i.e. reachable via layer2). if you don't get a response, you're probably not on the same network (of course with things like split horizon, static arp, there's no guarentee, but it would cover most cases)


________________________________
From: london.pm-***@groups.perlists.pm [london.pm-***@groups.perlists.pm] on behalf of Sue Spence [***@gmail.com]
Sent: 13 June 2018 12:33
To: Dave Hodgkinson
Cc: London PM
Subject: Re: Wrestling with IP addresses

Another pre-employment screening, I would guess. This is bit like swallows returning for summer. 🐊 🐊 (not swallows, but cute)

On Wed, 13 Jun 2018 at 11:57, Dave Hodgkinson <***@gmail.com<mailto:***@gmail.com>> wrote:

Please help a chap out.

I’ve been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP, Net::IP::Util and I’ve not been able to wrestle them into doing what I want.

Basically, if I have an address of 192.168.1.65 with a gateway of 192.168.1.254 I’d like to know if they’re on the same subnet. Same with 172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from the rfcs? Should I just check the first two bytes are the same? Wrong but trivial!

Ta,
Daniel Ruoso
2018-06-13 11:54:09 UTC
Permalink
Post by Dave Hodgkinson
Basically, if I have an address of 192.168.1.65 with a gateway of
192.168.1.254 I’d like to know if they’re on the same subnet. Same with
172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from
the rfcs? Should I just check the first two bytes are the same? Wrong but
trivial!
Without the network address and the network mask this is a malformed
question... You can inquire those two from the network device itself...
Uri Guttman
2018-06-13 16:10:21 UTC
Permalink
Post by Dave Hodgkinson
Please help a chap out.
I’ve been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP,
Net::IP::Util and I’ve not been able to wrestle them into doing what I
want.
Basically, if I have an address of 192.168.1.65with a gateway
of192.168.1.254I’d like to know if they’re on the same subnet. Same
with 172.16.10.65and 172.16.254.254for example. Is this trivial logic
from the rfcs? Should I just check the first two bytes are the same?
Wrong but trivial!
Ta,
Net::Netmask is your friend. it has a match method to determine if an IP
is on a network. you can set the netmask to any bit size you want.

uri
Dave Hodgkinson
2018-06-14 15:56:55 UTC
Permalink
Sys::HostAddr <https://metacpan.org/pod/Sys::HostAddr> looks like the best way of getting my IP address and mask.
Post by Dave Hodgkinson
Please help a chap out.
I’ve been wrestling with NetAddr::IP::Lite, NetAddr::IP, Net::IP <net::IP>, Net::IP::Util <net::IP::Util> and I’ve not been able to wrestle them into doing what I want.
Basically, if I have an address of 192.168.1.65 with a gateway of 192.168.1.254 I’d like to know if they’re on the same subnet. Same with 172.16.10.65 and 172.16.254.254 for example. Is this trivial logic from the rfcs? Should I just check the first two bytes are the same? Wrong but trivial!
Ta,
Net::Netmask <net::Netmask> is your friend. it has a match method to determine if an IP is on a network. you can set the netmask to any bit size you want.
uri
Loading...