http://qs1969.pair.com?node_id=211264

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

Hey, I am writing a little piece of code to check the sanity of an IP address. I haven't got it to break yet, so I was wondering if anyone else would have a go and see if there is anything wrong. Thanks!

#!/usr/bin/perl use strict; print "What IP address/Domain Name do you want to access: "; $_ = <>; my $ip = SanitizeIP($_); print "$ip is sane.\n"; exit; sub SanitizeIP { chomp ($_); my ($a,$c,$d,$e,$f); my $b = 0; if ($_ =~ /[a-zA-Z]+/) { print "$_\n"; $_ = gethostbyname($_); ($c,$d,$e,$f) = unpack('C4', $_); $_ = $c . "." . $d . "." . $e . "." . $f; } my @_temp = split(/\./,$_); undef $_; foreach $a(@_temp) { if ($a =~ /\b\d\b|\b\d\d\b|\b\d\d\d\b/) { if (($a >= 0) and ($a <= 255)) { $_ = $_ . int($a); $_ = $_ . "." unless ($b > 2); $b++; } else { print "This is an illegal IP address -- or it sure see +ms like one.\n"; exit; } } else { print "This is an illegal IP address -- or it sure seems l +ike one.\n"; exit; } } if (($_ eq "") or ($_ eq " ")) { print "The Address did not resolve, or there was something wro +ng with what you typed.\n"; exit; } return $_; }