Discipulus has asked for the wisdom of the Perl Monks concerning the following question:

hello there
I producede the following raw Perl code. can someone be so polite to tell me if it trow away all unwanted no public and valid IP in the dot-decimal notation?
Improvements welcome

my $ip = $ARGV[0]; if ( defined $ip && $ip=s/\s+// && $ip=~/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ && $ip=~/ˆ([01]?\d\d?<2[0-4]\d<25[0-5])\./&& $ip!~/^172\.0?(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\./ +&& #### MMMHHHHHHH $ip!~/^0\.|^0?10\./ && #010.32.1.1 ??? as argv con /^1?0\./ $ip!~/^192\.168\./ ) { # stuff here }
thanks
L*

there are no rules, there are no thumbs..

Replies are listed 'Best First'.
Re: ugly filtering valid public IP with regexes
by afoken (Chancellor) on Dec 28, 2011 at 13:45 UTC

    Hmmm, why don't you convert any VALID IP address to a four byte integer and test that against four or five bit patterns (127.0.0.0/8, 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12, and perhaps 169.254.0.0/16)?

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: ugly filtering valid public IP with regexes
by Anonymous Monk on Dec 28, 2011 at 13:59 UTC
Re: ugly filtering valid public IP with regexes
by umasuresh (Hermit) on Dec 28, 2011 at 13:40 UTC