Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Script to input IP Address and Subnet Bits (10.3.2.0/28)

by jesuashok (Curate)
on Dec 03, 2005 at 15:11 UTC ( #513811=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have this one, but it only accepts 10.3.2.1 (as well as www.example.com).

if (($qs =~ /^(\d+\.\d+\.\d+\.\d+)$/) or ($qs =~ /^([a-zA-Z0-9][a-zA-Z0-9\.\-_]*)$/)) { $qs = $1;
Any suggestions?

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: Script to input IP Address and Subnet Bits (10.3.2.0/28)
by psychotic (Beadle) on Dec 03, 2005 at 15:36 UTC
    Hello. I only have a suggention on the IP matching part. I've always found myself using the below extra-large regexp, courtesy of Mastering Regular Expressions.
    my @ips = qw( 127.0.0.1 0.0.0.0 256.0.0.1 123.555.333.222666 ); foreach my $ip (@ips) { my $f = undef; if($ip =~ m/^(?!0+\.0+\.0+\.0) ([01]?\d\d?|2[0-4]\d|25[0-5]) \.([01]?\d\d?|2[0-4]\d|25[0-5]) \.([01]?\d\d?|2[0-4]\d|25[0-5]) \.([01]?\d\d?|2[0-4]\d|25[0-5])$/x ) { $f =1; } print 'Is', $f ? "" : 'n\'t', " valid and/or usable IP: $ip \n" }
    When run produces:
    Is valid and/or usable IP: 127.0.0.1 Isn't valid and/or usable IP: 0.0.0.0 Isn't valid and/or usable IP: 256.0.0.1 Isn't valid and/or usable IP: 123.555.333.222666
    Update: changed code to better illustrate its usefulness.
Re: Script to input IP Address and Subnet Bits (10.3.2.0/28)
by McDarren (Abbot) on Dec 03, 2005 at 15:40 UTC
      It doesn't check for range, so you can do:
      use Data::Validate::IP qw(is_ipv4); my $ip_w_mask = '10.3.2.0/28'; my ($ip, $mask) = m{^(.*)/(\d+)$} or die("Incorrect format: Mask not found\n"); is_ipv4($ip) or die("Incorrect format: Malformed IP address\n");
Re: Script to input IP Address and Subnet Bits (10.3.2.0/28)
by Joost (Canon) on Dec 03, 2005 at 15:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://513811]
Approved by Joost
help
Chatterbox?
[Discipulus]: if i can suggest La Reine Margot too is a great book and obviously The Count of Monte Cristo that is a chef-d'œuvre
[Discipulus]: The red sphinx is about Rechilieu, absolutely thrilling
[erix]: thanks, Discipulus - I got them lined up at gutenberg :)
[erix]: I red the musketeers and Monte Cristo but both a very long time ago (many decades ago)
[erix]: *read
[Discipulus]: dunno if a young people can well understand the deep of Montecristo

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-05-24 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?