I have list of IPs:
238.51.208.96/28
238.51.209.180-199
238.51.209.100-109
238.51.213.2-254
...
How can I easily parse them? I need first and last IP from range.
For the first line I can use Net::Netmask CPAN module, but what can I do with others lines?
views:
465answers:
2
+5
A:
Try Net::IP module
If second patterns does not support, you may need to some changes to ips in advances like
238.51.209.180-199
to
238.51.209.180 - 238.51.209.199
by using some regex, for example,
$range =~ s/^((?:\d+\.){3})(\d+)-(\d+)$/$1$2 - $1$3/gm;
Full script:
use warnings;
use strict;
use Net::IP;
my $range = "238.51.209.180-199";
$range =~ s/^((?:\d+\.){3})(\d+)-(\d+)$/$1$2 - $1$3/;
my $ip = new Net::IP ($range) || die;
print $ip->ip (), "\n";
print $ip->last_ip (), "\n";
S.Mark
2010-04-13 01:33:58
`my $ip = new Net::IP ("238.51.209.180-199") || die;` output is `Died at ./andrey-zentavr.pl line 5.`
Kinopiko
2010-04-13 01:46:02
@Kinopiko - added a workaround.
S.Mark
2010-04-13 01:47:10
Kinopiko
2010-04-13 01:49:01
Thank you. I have added /gm too, I think its need multiline flag
S.Mark
2010-04-13 01:49:24
I do not want my script to die.If Net::IP($range) fails, I need to remake string to clear format.
Andrey Zentavr
2010-04-13 02:05:14
Just change the script then. It's only an example.
Kinopiko
2010-04-13 02:15:18
+3
A:
You can match IP addresses using the Regexp::Common::net package, and manipulate them (and get netmasks etc) with any number of modules on CPAN, including Network::IPv4Addr, NetAddr::IP and Net::CIDR.
Ether
2010-04-13 02:20:09
I desided to use smt like this:# Check, what range format do we have? if($ipline =~ /^((?:\d+\.){3})(\d+)-(\d+)$/){ $ipline =~ s/^((?:\d+\.){3})(\d+)-(\d+)$/$1$2 - $1$3/; }
Andrey Zentavr
2010-04-14 14:50:33