H
Hongyi Zhao
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?
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?