Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Validating IP address

    Hello all,

    I am using following regex to validate ip address:

    / ^([1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])((\.\d{0,2}|1\d{2}|2[0-4]\d|25[0-5]){3})$ /

    Its based on following:
    # For valid IP address (in IPv4) is that, it should be form of P. Q .R . S , with value of Q, R, S in [0-255] and P in (0-255].
    # Also IP addess as 1.001.12.34 is anot allowed [leading zeroes not allowed]
    # So the ip address lie from 0-255, i.e. 0-99, 100- 199, 200- 255 [P from 1 - 255]

    Also I would appreciate if someone can point out mistake in my regex rather than just giving the code in the start. This will help me clear my basics as I am pretty new to it. Many thanks.

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Take a look at the module Data::Validate::IP. You can examine the source code for the function is_ipv4 to learn how they do it.

    Also, googling validating ip address in perl provides this other forum post.

    - Miller

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by viczsaurav View Post
    Hello all,

    I am using following regex to validate ip address:

    / ^([1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])((\.\d{0,2}|1\d{2}|2[0-4]\d|25[0-5]){3})$ /

    Its based on following:
    # For valid IP address (in IPv4) is that, it should be form of P. Q .R . S , with value of Q, R, S in [0-255] and P in (0-255].
    # Also IP addess as 1.001.12.34 is anot allowed [leading zeroes not allowed]
    # So the ip address lie from 0-255, i.e. 0-99, 100- 199, 200- 255 [P from 1 - 255]

    Also I would appreciate if someone can point out mistake in my regex rather than just giving the code in the start. This will help me clear my basics as I am pretty new to it. Many thanks.
    i don't think i'm able to point out your mistakes,

    building a regex for validaiting ip is more a mental execise then
    something usefull because you can solve the problem faster if you
    don't use regex,

    each P,Q,R,S are integer between 0-255 so you need:
    1. 0-9 that means \d
    2. 10-99 that means [1-9]\d
    3. 100-199 that means 1\d\d or 1\d{2}
    4. 200-249 that means 2[0-4]\d
    5. 250-255 that means 25[0-5]

    if you combine them with | will be

    \d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]

    and if you name this Expr an ip will be Expr(\.Expr){3}

    finaly you have:

    /^(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$/

    Edit: two things:
    - look to the links posted by miller
    - passing this test doesn't mean that ip is valid,

    best regards
    Last edited by oesxyl; 03-18-2011 at 03:04 AM.

  • Users who have thanked oesxyl for this post:

    viczsaurav (03-18-2011)

  • #4
    New to the CF scene
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hmmmm.. Thanks a ton,that was quite explanatory, let me check my code too and get back.
    Also when you said "building a regex for validaiting ip is more a mental execise then
    something usefull because you can solve the problem faster if you
    don't use regex", did you mean using module in perl or there is some other ways of doing it too?

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by viczsaurav View Post
    Hmmmm.. Thanks a ton,that was quite explanatory, let me check my code too and get back.
    Also when you said "building a regex for validaiting ip is more a mental execise then
    something usefull because you can solve the problem faster if you
    don't use regex", did you mean using module in perl or there is some other ways of doing it too?
    yes and no, using perl modules is usualy a good idea because you often need to do more things then a simple check if a ip is syntactic correct and because you not spend so much time writing/testing/fixing a complicated regex.
    other way is to simple split the string into pices and assure that are integers between 0 and 255.
    for example:
    Code:
    sub check_ip {
      my @nums = split /\./, shift;
      return 0 if scalar @nums != 4;
      foreach my $num (@nums){
         return 0 if int($num) < 0 || int($num) > 255;
      }
      return 1;
    }
    i guess anybody could write this faster then the regex solution no matter how confortable is with regex,

    best regards

  • #6
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    oesxyl,

    You're code does not completely validate strings like "12bob.1.1.1". No matter though, your code does closely match that of the cpan module.

    viczsaurav,

    It's best to use a module for this type of validation for a few reasons. One, it's the quickest solution to implement across multiple projects since you can just use the installed cpan module instead of copying code or some other nonsense. Two, it self documents, since a function like is_ipv4 is pretty self explanatory. Three, these functions will often pick up cases that you may not know about, and if you do discover something you can file a bug report and everyone will benefit from your insight. Four, these modules often perform operations that you'd never consider, like this one does taint validation of the data. May not be useful now, but could be in the future.

    Either way, do these exercises for fun if you'd like, but I'd definitely recommend that you learn to take advantage of cpan modules, even for something as apparently simple as ip validation.

    - M

  • Users who have thanked miller for this post:

    viczsaurav (03-19-2011)

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by miller View Post
    oesxyl,

    You're code does not completely validate strings like "12bob.1.1.1". No matter though, your code does closely match that of the cpan module.
    you mean the check_ip sub from my previous post? probably you are right, is my fault, i write it as an example, i realise that was not too smart,

    viczsaurav,

    It's best to use a module for this type of validation for a few reasons. One, it's the quickest solution to implement across multiple projects since you can just use the installed cpan module instead of copying code or some other nonsense. Two, it self documents, since a function like is_ipv4 is pretty self explanatory. Three, these functions will often pick up cases that you may not know about, and if you do discover something you can file a bug report and everyone will benefit from your insight. Four, these modules often perform operations that you'd never consider, like this one does taint validation of the data. May not be useful now, but could be in the future.

    Either way, do these exercises for fun if you'd like, but I'd definitely recommend that you learn to take advantage of cpan modules, even for something as apparently simple as ip validation.

    - M
    be resonable, i don't think that downloading tons of garbage from cpan is the best solution to solve simple problems,
    Like all software from today perl suffer from dependences problems, bad code and developers indifference. What was true few years ago is not necesarely true today.

    best regards

  • #8
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by oesxyl View Post
    be resonable, i don't think that downloading tons of garbage from cpan is the best solution to solve simple problems,
    agreed, and yet I stand my point in this instance anyway

    - M

  • #9
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    i don't think i'm able to point out your mistakes,

    building a regex for validaiting ip is more a mental execise then
    something usefull because you can solve the problem faster if you
    don't use regex,

    each P,Q,R,S are integer between 0-255 so you need:
    1. 0-9 that means \d
    2. 10-99 that means [1-9]\d
    3. 100-199 that means 1\d\d or 1\d{2}
    4. 200-249 that means 2[0-4]\d
    5. 250-255 that means 25[0-5]

    if you combine them with | will be

    \d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]

    and if you name this Expr an ip will be Expr(\.Expr){3}

    finaly you have:

    /^(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$/

    Edit: two things:
    - look to the links posted by miller
    - passing this test doesn't mean that ip is valid,

    best regards
    Hello.

    And what about when lenght is computed from address 1 to 254 (255 is broadcast) ?

    ?

    I have script which counts lenght of IP

    #!/usr/bin/perl

    $start=$ARGV[0]; # Starting IP
    $end=$ARGV[1]; # Ending IP

    sub usage() { # Usage printing function.
    print "$0 <startIP> <endIP>
    both startIP and endIP must be IPs.
    The result is the number of IP from startIP, included to endIP, included.\n";
    exit 12;
    }

    sub isIP($) { # This function takes an IP, and returns the value of the IP in hex
    $IP=shift; # If the input is not an IP, then the return values is invalid.
    @nums = $IP =~ m/^([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-3]\d|25[0-4])$/;
    @hex=map(sprintf("%02x",$_),@nums);
    return join('',@hex);
    }

    my $startparts=isIP($start) or usage();
    my $endparts=isIP($end) or usage();

    print hex($endparts)-hex($startparts);
    but when I entered:

    start ip is - 10.52.33.65
    end ip is - 10.52.33.78

    and the results are
    13

    but when i entered:

    10.52.60.1
    10.52.61.254

    is 509

    is it correct for when lenght is computed from address 1 to 254 (255 is broadcast) ?

    please help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •