Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to check for ip address that falls inside a range.

Reply
Thread Tools

how to check for ip address that falls inside a range.

 
 
mike
Guest
Posts: n/a
 
      01-30-2004
hi

i need to verify if an ip-address falls inside a range for example,
say 10.1.1.1 to 10.1.2.255.
So if a user keys in 10.1.2.2, it should fall inside this range and
then the program can do something.

How can i go about declaring the range, and how can i check the ip
whether it falls inside the range. ?
thanks.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-30-2004
In article <> ,
mike <> wrote:
:i need to verify if an ip-address falls inside a range for example,
:say 10.1.1.1 to 10.1.2.255.
:So if a user keys in 10.1.2.2, it should fall inside this range and
:then the program can do something.

:How can i go about declaring the range, and how can i check the ip
:whether it falls inside the range. ?

for each of the three IP addresses, split the IP address on '\.',
say into @ipd; then
$iaddr = pack('C4', $ipd[0], $ipd[1], $ipd[2], $ipd[3]);
gives the binary equivilent of the IP. Once you have all three,
it's just a matter of testing that the test ip is numerically
between the other two values.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-30-2004
mike wrote:
> i need to verify if an ip-address falls inside a range for example,
> say 10.1.1.1 to 10.1.2.255.


One idea:

my $test = '10.1.2.2';
my $lower = '10.1.1.1';
my $higher = '10.1.2.255';

sub iptest {
my @ip = @_;
for ( @ip ) {
$_ = join '', map { sprintf '%03s', $_ } split /\./, $_;
}
$ip[0] >= $ip[1] and $ip[0] <= $ip[2] ? 1 : 0;
}

if ( iptest($test, $lower, $higher) ) {
print "$test falls inside the range $lower to $higher.\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      01-30-2004

(Walter Roberson) wrote:
> In article <> ,
> mike <> wrote:
> :i need to verify if an ip-address falls inside a range for example,
> :say 10.1.1.1 to 10.1.2.255.
> :So if a user keys in 10.1.2.2, it should fall inside this range and
> :then the program can do something.
>
> :How can i go about declaring the range, and how can i check the ip
> :whether it falls inside the range. ?
>
> for each of the three IP addresses, split the IP address on '\.',
> say into @ipd;


Or, better, use Regexp::Common.

Ben

--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* *
 
Reply With Quote
 
Tony Curtis
Guest
Posts: n/a
 
      01-30-2004
>> On 29 Jan 2004 18:43:28 -0800,
>> (mike) said:


> hi i need to verify if an ip-address falls inside a range
> for example, say 10.1.1.1 to 10.1.2.255. So if a user keys
> in 10.1.2.2, it should fall inside this range and then the
> program can do something.


> How can i go about declaring the range, and how can i check
> the ip whether it falls inside the range. ? thanks.


http://search.cpan.org/

Take a look at NetAddr::IP and Net::IP::Match, one or both
might do what you want.

hth
t
 
Reply With Quote
 
Kien Ha
Guest
Posts: n/a
 
      01-30-2004
mike wrote:
> hi
>
> i need to verify if an ip-address falls inside a range for example,
> say 10.1.1.1 to 10.1.2.255.
> So if a user keys in 10.1.2.2, it should fall inside this range and
> then the program can do something.
>
> How can i go about declaring the range, and how can i check the ip
> whether it falls inside the range. ?
> thanks.



#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket qw(inet_aton);


my @ip_range = qw{ 10.1.1.1 10.1.2.255 };
my ($lower, $upper) = map { inet_aton($_) } @ip_range;


for ( qw{ 10.1.2.2 10.1.2.10 10.1.3.222 } ) {
my $packed_ip = inet_aton($_);
if ( $packed_ip lt $lower or $packed_ip gt $upper ) {
print "$_ is out of expected range ",
"$ip_range[0] .. $ip_range[1].\n";
next;
}


print "$_ is in range.\n";
}


--
Kien

 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      01-30-2004
Kien Ha <> wrote in message news:<RCkSb.6281$ ble.rogers.com>...
> mike wrote:
> > hi
> >
> > i need to verify if an ip-address falls inside a range for example,
> > say 10.1.1.1 to 10.1.2.255.
> > So if a user keys in 10.1.2.2, it should fall inside this range and
> > then the program can do something.
> >
> > How can i go about declaring the range, and how can i check the ip
> > whether it falls inside the range. ?
> > thanks.

>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use IO::Socket qw(inet_aton);
>
>
> my @ip_range = qw{ 10.1.1.1 10.1.2.255 };
> my ($lower, $upper) = map { inet_aton($_) } @ip_range;
>
>
> for ( qw{ 10.1.2.2 10.1.2.10 10.1.3.222 } ) {
> my $packed_ip = inet_aton($_);
> if ( $packed_ip lt $lower or $packed_ip gt $upper ) {
> print "$_ is out of expected range ",
> "$ip_range[0] .. $ip_range[1].\n";
> next;
> }
>
>
> print "$_ is in range.\n";
> }





Thanks for all the tips...appreciate that..
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-31-2004
In article <RCkSb.6281$ ogers.com>,
Kien Ha <> wrote:
:mike wrote:
:> i need to verify if an ip-address falls inside a range for example,

:use IO::Socket qw(inet_aton);

Looking at perldoc, it appears to me that should be Socket instead
of IO::Socket .


: my $packed_ip = inet_aton($_);
: if ( $packed_ip lt $lower or $packed_ip gt $upper ) {

I realized a potential flaw in my pack 'C4' version:
I was using network byte order and numeric comparisions, which
would fail on small-endian or machines with other unusual binary
storage orders. I see that your version avoids that by doing
string comparisons instead of numeric comparisons. Think I'll
go edit some of my code now...
--
We don't need no side effect-ing
We don't need no scope control
No global variables for execution
Hey! Did you leave those args alone? -- decvax!utzoo!utcsrgv!roderick
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-31-2004
In article <bvfbsl$3g6$>,
Walter Roberson <> wrote:
|In article <RCkSb.6281$ ogers.com>,
|Kien Ha <> wrote:

|: my $packed_ip = inet_aton($_);
|: if ( $packed_ip lt $lower or $packed_ip gt $upper ) {

|I realized a potential flaw in my pack 'C4' version:
|I was using network byte order and numeric comparisions, which
|would fail on small-endian or machines with other unusual binary
|storage orders. I see that your version avoids that by doing
|string comparisons instead of numeric comparisons.

Ah, and I just found a theoretical flaw in the inet_aton version.
The documentation for inet_aton says that it returns an
opaque string, and that one should not assume that it is 32 bits
(e.g., because of IPv6.) Theoretically, then, the results of
inet_aton are not always going to be linear for lt / gt purposes,
even if the inputs are consistantly dotted quads.
--
Oh, yeah, an African swallow maybe, but not a European swallow.
That's my point.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-31-2004
Walter Roberson wrote:
> Ah, and I just found a theoretical flaw in the inet_aton version.
> The documentation for inet_aton says that it returns an opaque
> string, and that one should not assume that it is 32 bits (e.g.,
> because of IPv6.) Theoretically, then, the results of inet_aton are
> not always going to be linear for lt / gt purposes, even if the
> inputs are consistantly dotted quads.


So why not keep it simple, then?

http://groups.google.se/groups?selm=....uni-berlin.de



--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Gates falls on his face... Daniel Computer Support 8 01-09-2005 06:31 PM
Linux falls off DMZ Rick Wezowicz Cisco 3 04-12-2004 08:58 PM
HHEEEELLLPPP :o( .... My aspnet / sqlserver falls asleep???? M O J O ASP .Net 3 11-04-2003 06:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57