this comes from either the flow-tools or the cflow distribution, forget which. it's rarely failed me. it has the benefit of cacheing, flexible output. it's just plain cool.

#!/usr/usc/perl/5.6.1/bin/perl # ip2hostname - a filter to turn IP addresses into host names wherever + possible. # $Id: ip2hostname,v 1.8 2001/02/13 04:42:20 plonka Exp $ # Dave Plonka <plonka@doit.wisc.edu> use FindBin; use Socket; use Getopt::Std; sub usage { my $status = shift; print STDERR <<_EOF_ usage: $FindBin::Script [-h] [ -p printf_format ] [ [-i extension] fil +e [...] ] -h - help (shows this usage information) (mnemonic: 'h'elp) -p printf_format - use this printf format for IP address and ho +stname, respectively. The default format is '%.0s%s', which supresses the printing of the IP address (i.e. "%.0s" specifies printing a string with a maximum width of zero). To maintain column widths (since both the IP address and hostname vary in length), a format like this may be useful: '%-15.15s %-20s' (mnemonic: 'p'rintf format) -i extension - edit the files in place (rather than sending to standard output) This option requires file name(s) argument(s). The extension is added to the name of the old file to make a backup copy. If you don't wish to make a backup, use "-I". (mnemonic: edit 'i'n place) -I - like "-i" but no backup is made. (mnemonic: edit 'I'n place, trusting this script 'I'mplicitly. + ;^) _EOF_ ; exit $status } getopts('hp:Ii:') || usage(2); usage(0) if ($opt_h); $| = 1; my $oldargv; my %cache; while (<>) { # { this is straight from the "perlrun" man page: if ('-' ne $ARGV && ($opt_I || $opt_i) && $ARGV ne $oldargv) { if ('' eq $opt_i) { unlink($ARGV) or die "unlink \"$ARGV\": $!\n" } else { rename($ARGV, $ARGV . $opt_i) or die "rename \"$ARGV\": $!\n" } open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } # } my $s = $_; my $prev = ''; my($name, $val); while ($s =~ m/(\d{1,3})(?:\.\d{1,3}){3}/) { my $ip = $&; $s =~ s/$1\.//; next if ($ip eq $prev); if (defined($cache{$ip})) { $name = $cache{$ip} } else { $name = gethostbyaddr(inet_aton($ip), AF_INET); $cache{$ip} = $name } if ('' eq $name) { $name = $ip } if ($opt_p) { $val = sprintf($opt_p, $ip, $name) } else { $val = $name } s/$ip/$val/g; $prev = $ip } print }

In reply to Re: perl to convert text list of ip address to a name by Anonymous Monk
in thread perl to convert text list of ip address to a name by lola_marais

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.