Posted By

numberwhun on 06/05/07


Tagged

validation perl ipAddress


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

iNiels


Verify if an IPv4 IP address is valid


 / Published in: Perl
 

This code will validate not only the four octets contain between 1 and 3 numbers each, but also that the number they contain is between 0 and 255.

In all, there are 3 different regex's that I tried and all seem to work fine. The three regex's are:

  1. m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)/
  2. m/\d\d?\d?.\d\d?\d?.\d\d?\d?.\d\d?\d?/
  3. m/\d{1,2}.\d{1,3}.\d{1,3}.\d{1,3}/

The first one is used below. Enjoy!!!

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6.  
  7. print("What is the IP Address you would like to validate: ");
  8. my $ipaddr = <STDIN>;
  9. chomp($ipaddr);
  10.  
  11.  
  12. if( $ipaddr =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/ )
  13. {
  14. print("IP Address $ipaddr --> VALID FORMAT! \n");
  15.  
  16. if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
  17. {
  18. print("IP address: $1.$2.$3.$4 --> All octets within range\n");
  19. }
  20. else
  21. {
  22. print("One of the octets is out of range. All octets must contain a number between 0 and 255 \n");
  23. }
  24. }
  25. else
  26. {
  27. print("IP Address $ipaddr --> NOT IN VALID FORMAT! \n");
  28. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: phalek on July 9, 2007

That's surely true for a string like ' 172.168.0.1' ( not in valid format ) but '172.168.0.1 ' will be in valid format. You'll have to add a '$' to the end of each regex ...

e.g.: m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)$/

But still, thanks for that regex, that's exactly what I needed to create a validate ip script without using a cpan module (e.g Data::Validate::IP).

Posted By: numberwhun on July 16, 2007

Thanks for the missing '$'. I will be sure and add it. Glad that it helped you with your project as well!

Posted By: albstp on April 16, 2008

Hi Thanks for posting this script. It was so helpful. Iam using it in my project.

Posted By: sharmashashee on September 9, 2008

Hi,

To validate IPv4 addressing above code has a bug.

Regexp defined above is ( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)/ )

but for example IP provided is 1.12.123.1234, with above expression it is a valid IP address.

this is the correct regexp for validating Ipv4 addressing: ( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})/ )

Anyways thanks for the code. It is helping me alot.

Posted By: j0sh097 on January 8, 2009

( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})/ ) this will let something like 1.1.1.1e slip by. Instead add $ to the end ( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})$/ )

Posted By: dhineshkumar on August 12, 2010

I did a script which satisfy the condition : 0 to 255. You can easily edit the script based on ur network class.

The below script will help u to some extent with help of regular expression.

!/usr/bin/perl

Pass the IP Address details

$ip = $ARGV[0];

if( $ip =~ /^((([01]?[0-9]{1,2}|[2][0-5]{1,2}).){3}([01]?[0-9]{1,2}|[2][0-5]{1,2}))$/ ) { print "IP:$ip: is valid\n"; } else { print "IP:$ip: is not valid\n"; }

Posted By: veljkopopovic on August 26, 2010

You should prefer cpan packages if any .... In this case, there is one, so my recommendation is to use Data::Validate::IP to validate your string ... Here is link to docs: http://search.cpan.org/~neely/Data-Validate-IP-0.11/lib/Data/Validate/IP.pm

Posted By: numberwhun on November 12, 2010

@veljkopopovic Your absolutely right, there is a module for that. But, at the time that I put this together I was learning and posted what was working for me. I preferred to re-invent the wheel, that's why I wrote this.

@j0sh097 Good point on the $. I never actually came back to this and corrected it. I did notice that fact and corrected it in my local scripts where I used it, but never updated it here. Thanks for pointing that out.

@dhineshkumar I would still chomp $ARGV as a "just in case", but that's me. :) Thanks!

Posted By: numberwhun on November 12, 2010

@j0sh097 Just wanted to note that the version I used in the code actually catches the 1.1.1.1e and doesn't let it pass. The one you mentioned does let it slip, so I am glad I used the one I did. :)

use warnings;

print("What is the IP Address you would like to validate: "); my $ipaddr = ; chomp($ipaddr);

if( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)$/ ) { print("IP Address $ipaddr --> VALID FORMAT! \n");

if($1
Posted By: sv on November 25, 2010

Hello All,

discovered for dhineshkumar's example :
IP:10.10.10.1: is valid

IP:11111111: is valid

IP:172.16.0.248: is not valid

my modification is :

!/usr/bin/perl

Pass the IP Address details

$ip = $ARGV[0];

if(( $ip =~ /^((([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/ ) { print "IP:$ip: is valid\n"; } else { print "IP:$ip: is not valid\n"; }

Posted By: nokuntrol on December 20, 2011

sv posted a really great solution. It's clean, but I found a bugs with the code. 1) IP cannot start with 0 (zero). minor fix: /^((([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/

Then a nit pick (not really a bug), but if you think that an octet cannot start with 0 (zero) and then follow with additional digits. ie..192.168.1.023 or 192.168.1.00 fix: /^((([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([0]|[1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/

Posted By: MIrage7721 on November 14, 2012

Hi i like the script going to use it in a project. thank you for posting it

Posted By: MIrage7721 on November 14, 2012

Hi i like the script going to use it in a project. thank you for posting it

Posted By: MIrage7721 on November 14, 2012

Hi i like the script going to use it in a project. thank you for posting it

Posted By: shajay12 on February 15, 2013

Hi Friends, with the above regular expression, it will also match 0.0.0.0 so tell me a code that will not match any ip address which has "0" in it.

Posted By: petelock on June 2, 2013

I like this one using quoted reg ex

my $ip = '192.168.255.254'; # for example

Set up the quoted regex

my $ipno = qr/ 2(?:5[0-5] | [0-4]\d) | 1\d\d | [1-9]?\d /x;

To test an IP address

if ( $ip =~ /^($ipno(?:.|$)){4}/ ){ print "IP OK\n"; };

I went for an interview at Arm in Cambridge, UK. They asked me to write one on the board and I wrote some lame one .. and later ... brooding on my poor attempt set out to make a better one. Driven by failure? Or maybe just really irritated by it. I still got the job :)

You need to login to post a comment.