Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 $_; }

In reply to IP Address Sanity by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found