Perl IP to Domain Name converter.

Discussion in 'Perl Misc' started by Hongyi Zhao, Jan 31, 2009.

  1. Hongyi Zhao

    Hongyi Zhao Guest

    Hi all,

    I find a Perl IP to Domain Name converter from the following url:

    http://www.osix.net/modules/article/?id=194

    I copy the code to a perl script named PerlIP2DomainName.pl, when I
    run it I meet the errors like this:
    $ ./PerlIP2DomainName.pl
    Global symbol "$infilePlacing" requires explicit package name at
    ../PerlIP2Domain
    Name.pl line 28.
    syntax error at ./PerlIP2DomainName.pl line 31, near "=)"
    syntax error at ./PerlIP2DomainName.pl line 41, near "}"
    Execution of ./PerlIP2DomainName.pl aborted due to compilation errors.
    Here is the perl code:

    #!/usr/bin/perl -w
    use Socket;
    use Getopt::Std;
    use strict;
    # simple script to resolve ip addresses in a web log file
    # into host names
    # typical logline:
    # 213.123.213.254 - - [11/Feb/2002:13:29:14 +0000] "GET
    /phppolls/phppolls.php?poll_action=viewPoll&poll_id=22 HTTP/1.1" 200
    5394 "http:#www.sonictown.co.uk/main/main.php" "Mozilla/4.0
    (compatible; MSIE 6.0; Windows NT 5.0)"

    my %options;
    my ($infile, $outfile, %cached_ips, $line, @lines, $tmp, @bits,
    $newline, @newlist);
    my $progname=$0;
    # use basename only:
    $progname=~s,.*/,,;
    # parse our arguments:
    getopts("dho:vf:", %options);
    # if no infile, print usage:
    if(!$options{f} || $options{h}){usage();}
    $infile=$options{f};
    # if no outfile given, default to $infile."new":
    if(!$outfile){$outfile=$infile.".new";}

    # attempt to open infile:
    open(IN, $infile) || die("Unable to open file: $infile");
    # attempt to open outfile:
    open(OUT, ">$outfile") || die("Unable to open file: $outfile");
    if($options{v}){
    print "Taking input from: $infilePlacing output to: $outfile";
    sleep 2;
    }
    while($line=){
    # get the ip and the date parts from the lines:
    @bits=split(" ", $line);
    if($options{d}){ print $bits[0].""; }

    $bits[0]=resolve_ip($bits[0]);
    # put line back together:
    print OUT $newline=(join(" ", @bits)."");
    # if verbose set, print each line out to stdout:
    if($options{v}){print $newline;}
    }
    if($options{v}){ print "".%cached_ips; }
    sub resolve_ip{
    my($this_ip);
    foreach $this_ip (keys %cached_ips){
    # if this ip has already been cached, return the name,
    otherwise resolve and cache this ip/name:
    $_[0] eq $this_ip ? return($cached_ips{$this_ip}) :"";
    }
    return(cache_ip($_[0]));
    }
    sub cache_ip{
    my $name;
    # attempt to resolve ip:
    if( ($name= gethostbyaddr(inet_aton($_[0]), AF_INET)) eq "" ){
    return($cached_ips{$_[0]}=$_[0]);
    } else {
    return($cached_ips{$_[0]}=$name);
    }
    }
    sub usage{
    die [-o ]
    -f REQUIRED - name of input log file to resolve dotted ip
    addresses to canonical names.
    -o OPTIONAL - name of output file to dump results to.
    Defaults to .new.
    -v OPTIONAL - verbose; prints out each logfile line as it

    is parsed.
    EOF
    }

    __END__

    Any hints on this issue?
     
    Hongyi Zhao, Jan 31, 2009
    #1
    1. Advertisements

  2. You don't know Perl at all, do you?

    You have no intention of learning Perl, do you?

    You just want someone else to write you a script, don't you?

    Syntax errors like that are easy to diagnose and fix if you know a
    little bit of Perl.

    However, I don't see the point of spending time on the particular script
    you copied from some web site.

    If all you want is a log file processor, there are oodles of them on the
    interweb.

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Jan 31, 2009
    #2
    1. Advertisements

  3. Hongyi Zhao

    Hongyi Zhao Guest

    You're absolutely correct! At least for now so:)
    Shame on me, I'm just a very newbie and can not take much time to
    learn it presently.
     
    Hongyi Zhao, Jan 31, 2009
    #3

  4. If there are syntax errors, then what you have is not a Perl program...


    This is the first mention of $infilePlacing in the program.

    It has not been declared, which violates the "use strict" pragma and
    produces the first error message above.

    Furthermore, it has not been given any value, so outputting it is not likely
    to be productive anyway.


    Perhaps this was meant to be:

    while ($line = <IN>) {


    Clearly this program has never ever been executed.

    There are lots of bad "programs" on the wild interweb, you have found one
    of them.

    Even without the syntax errors, I can say that this was not written
    by a good Perl programmer...


    What you have found is garbage.

    Keep looking.
     
    Tad J McClellan, Jan 31, 2009
    #4
  5. HZ> On Sat, 31 Jan 2009 13:55:07 GMT, "A. Sinan Unur"

    HZ> You're absolutely correct! At least for now so:)


    HZ> Shame on me, I'm just a very newbie and can not take much time
    HZ> to learn it presently.

    Indeed. Shame on you.

    You have two options. You can learn Perl, or you can hire a programmer.

    For the former option, http://www.perl.org/books/beginning-perl/

    For the latter option, http://jobs.perl.org/

    In either case, good luck.

    Charlton
     
    Charlton Wilbur, Jan 31, 2009
    #5
  6. http://www.osix.net/modules/article/?id=194

    Ick!
     
    RedGrittyBrick, Feb 2, 2009
    #6
  7. Ick! is right. For many reasons. Here is one:

    <quote src="http://www.osix.net/modules/article/?id=194">
    sub resolve_ip {
    my($this_ip);
    foreach $this_ip (keys %cached_ips) {
    $_[0] eq $this_ip ? return($cached_ips{$this_ip}) :"";
    }
    return(cache_ip($_[0]));
    }

    sub cache_ip {
    my $name;
    if( ($name= gethostbyaddr(inet_aton($_[0]), AF_INET)) eq "" ){
    return($cached_ips{$_[0]}=$_[0]);
    } else {
    return($cached_ips{$_[0]}=$name);
    }
    }
    </quote>

    This is insanely bad. Here is something less bad:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Memoize;
    use Socket;

    memoize( 'resolve_ip' );

    while ( <DATA> ) {
    chomp;
    last unless length;
    print "Looking up '$_'\n\t";
    print resolve_ip( $_ ), "\n";
    }

    sub resolve_ip {
    my ($ip) = @_;

    my $name = gethostbyaddr inet_aton($ip), AF_INET;

    return $name if defined $name;
    return $ip;
    }

    __DATA__
    74.125.45.100
    68.180.206.184
    207.46.197.32
    74.125.45.100
    68.180.206.184
    207.46.197.32

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Feb 3, 2009
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.