Home > Archive > PERL Miscellaneous > May 2004 > validating IP addresses
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
validating IP addresses
|
|
|
| I need a function to accept an IP address (V4 or V6) and return if it is
valid or not.
--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
| |
| Stuart Moore 2004-05-18, 3:42 pm |
| Bob wrote:
> I need a function to accept an IP address (V4 or V6) and return if it is
> valid or not.
>
Define valid?
If it's v4, then either do something like
/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ && $1<=255 && $2<=255 && $3
<= 255 && $4<=255;
(although I think you might also want to check for no 0s in certain places)
or if you want to find out if it's an allocated one, some kind of dns
query - or maybe just ping.
v6 I guess is similar, although the regexp will look different. There's
possibly a module that does this for you
Stuart
| |
| John W. Krahn 2004-05-18, 5:32 pm |
| Bob wrote:
>
> I need a function to accept an IP address (V4 or V6) and return if it is
> valid or not.
perldoc Net::CIDR
[snip]
$ip=Net::CIDR::cidrvalidate($ip);
Validate whether $ip is a valid IPv4 or IPv6 address.
Returns its argument or undef. Spaces are removed, and
IPv6 hexadecimal address are converted to lowercase.
John
--
use Perl;
program
fulfillment
| |
|
| On 05/18/04 14:31 Stuart Moore spoke:
> Bob wrote:
>
>
> Define valid?
Just all numbers less than 256, etc.
Like what you wrote. :-)
Thanks.
>
> If it's v4, then either do something like
>
> /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ && $1<=255 && $2<=255 && $3
> <= 255 && $4<=255;
>
> (although I think you might also want to check for no 0s in certain places)
--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
| |
|
| On 05/18/04 15:25 Ben Morrow spoke:
> Quoth Bob <bob.lockie.NOSPAM@mail.com>:
>
>
>
> Regexp::Common has regexen for IPv4 addresses. I guess you could submit
> a patch to Abigail for IPv6.
>
> Ben
>
Thanks.
--
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.
| |
| Ben Morrow 2004-05-20, 6:31 pm |
|
Quoth Ben Morrow <usenet@morrow.me.uk>:
>
> Quoth Bob <bob.lockie.NOSPAM@mail.com>:
>
> Regexp::Common has regexen for IPv4 addresses. I guess you could submit
> a patch to Abigail for IPv6.
Hmmm... having had a go at this, I've managed to come up with a
5527-char regex as the simplest solution I can find... is there
something I'm missing?
#!/usr/bin/perl -l
use Regexp::Common;
my $byte = qr/[[:xdigit:]]{1,2}/;
my ($v6full, $v4in6) = map {
my $full = $_ - 1;
my $var = $_ - 3;
my @res = (
qr/ (?:$byte:){$full} $byte /x,
qr/ $byte? :: (?:$byte:){0,$var} $byte /x,
qr/ $byte (?: :$byte){0,$var} :: $byte? /x,
);
for my $i (3 .. ($_-3)) {
my $j = $_ - $i - 5;
push @res, qr/$byte : $byte : (?:$byte:){0,$i}
: (?:$byte:){0,$j} $byte : $byte/x;
}
\@res;
} (16, 10);
push @$v4in6, ':';
my $ff = qr/ff/i;
my @v4in6 = map {
qr/$_ : $ff :? $ff : $RE{net}{IPv4}/x
} @$v4in6;
$v4in6 = join '|', @v4in6;
$v6full = join '|', @$v6full;
my $IPv6 = qr/ $v4in6 | $v6full /x;
print $IPv6;
print length $IPv6;
while (<DATA> ) {
my ($addr, $valid) = split;
my $test = ($addr =~ /^$IPv6$/ ? 'valid' : 'invalid');
$test ne $valid and print "$addr : $test, should be $valid";
}
__END__
::1 valid
::FFFF:127.0.0.1 valid
::FF:FF:127.0.0.1 valid
::11:ff:127.0.0.1 invalid
0:0:0:0:0:0:0:0:0:0:0:ff:ff:127.0.0.1 invalid
0:0:0:0:0:0:0:0:0:0:Ff:fF:127.0.0.1 valid
1::ff:ff:255.255.255.255 invalid
::ff:FF:aa.0.0.0 invalid
:::1 invalid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16 valid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16: invalid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15::16 invalid
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:1
7 invalid
::0::1 invalid
1:1::1:1::1 invalid
:: invalid
a:b:c::d:e:f valid
a:b:c::d:e:f: invalid
Ben
--
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
-Assyrian stone tablet, c.2800 BC ben@morrow.me.uk
|
|
|
|
|