#!/usr/bin/perl # Valid private IP ranges my @LOCAL_IP_RANGES = ("10.0.0.0", "10.255.255.255", "172.16.0.0", "172.31.255.255", "192.168.0.0", "192.168.255.255"); # input: A dotted quad IP address. # output: 0 if a public (internet) address # : 8 if a class A private (LAN) address # : 12 if a class B private (LAN) address # : 16 if a class C private (LAN) address sub get_IP_number () { my ($dotted_quad) = @_; my $IP_number=0; split(/\./, $dotted_quad); for (my $i=0; $i < 4; $i++) { $IP_number=$IP_number+@_[3-$i]*(2**(8*$i)); } return $IP_number; } while(<>) { chop; my $IPnumber=&get_IP_number($_); my $class=16; while (@LOCAL_IP_RANGES) { my $highIPnumber = &get_IP_number(pop(@LOCAL_IP_RANGES)); my $lowIPnumber = &get_IP_number(pop(@LOCAL_IP_RANGES)); if (($lowIPnumber <= $IPnumber) && ($highIPnumber >= $IPnumber)) { exit($class); # PRIVATE IP } else { $class += 4; } } } exit(0); # PUBLIC IP