in reply to Checking IP's with an IP/mask
Here's some examples:#!/usr/bin/perl -w use strict; use Socket; sub MaskbitsToNetmask { my ($bits) = @_; return 0xFFFFFFFF-((1<<(32-$bits))-1); } sub MaskToRange { my ($range) = @_; my ($ip,$mask) = split (/\//, $range); $ip = CompactIP($ip); return ($ip, $ip | (1<<(32-$mask) - 1)); } sub ExpandIP ($) { return inet_ntoa (pack ("N", @_)); } sub CompactIP ($) { return unpack ("N", inet_aton (@_)); } sub IsInRange { my ($ip, $range) = @_; $ip = CompactIP($ip); my ($range_low, $range_hi) = MaskToRange ($range); return (($range_low <= $ip) && ($ip <= $range_hi)); }
Of course, this is just off the top, so YMMV.print IsInRange ("192.168.2.42","192.168.0.0/16"),"\n"; # 1 print IsInRange ("192.168.2.42","192.168.0.0/24"),"\n"; # undef print IsInRange ("192.168.2.42","192.168.2.0/24"),"\n"; # 1 print IsInRange ("24.123.234.1","24.0.0.0/8"),"\n"; # 1
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Checking IP's with an IP/mask
by repson (Chaplain) on Apr 24, 2001 at 09:40 UTC | |
by tadman (Prior) on Apr 24, 2001 at 18:49 UTC |