Abolish domain-names! Forget ip-addresses! Use easy-to-remember decimal numbers!
It's not exactly complicated code, but a lot of people don't know you can also use
decimals instead of ip-addresses: for 206.170.14.75, convert each element
into its binary equivalent, join the four strings and convert the resulting string
into decimal (which will be a number less than 2^32).
#!/usr/bin/perl -w
use strict;
use Socket;
for my $host (@ARGV) {
my $addr = inet_ntoa( scalar gethostbyname($host) );
my $bin;
$bin .= dec2bin($_) for (split /[.]/, $addr);
print bin2dec($bin), "\n";
}
sub bin2dec {
unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
sub dec2bin {
my $str = unpack("B32", pack("N", shift));
# we only need the lowest 8 bits (ipv4 range 0-255)
return substr $str, length($str) - 8;
}
Example:
[ar0n@zendo perl]% perl ip2dec.pl perlmonks.org
3467251275
Now load up your browser and be amazed! (your perlmonks cookie will, of course, not work)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.