1834293 Members
2150 Online
110066 Solutions
New Discussion

Perl script

 
robert_177
Occasional Contributor

Perl script

I am writing a script to parse web logs, this script captures IP, DATE and TIME and
Requested page in $host, $date, $request variables. How do I capture
"http://www.portallink.com/finder.jhtml" in another variable $url in the same way.

Script:
#!/usr/bin/perl -w

$logfile = shift || &usage;

# forward declarations
my ($host, $date, $request);

# open the log
open(LOG, "<$logfile") or die "could not open $logfile\n";

while() {
($host, $date, $request) =
$_ =~ m{
(.*?)\s # host name or IP
\[(.*?)\]\s # date
\"(.*?)\"\s # request method
}x;

print "$host $date $request \n";
}

Sample web log
216.177.64.101 - - [09/Apr/2003:00:00:41 -0400] "GET /logout.jhtml HTTP/1.0" 200 15758 "http://www.portallink.com/finder.jhtml" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" "ProfileCookie=demositelite,XYZ Company, Inc.,; JSESSIONID=OCZA0EYAAACSYCQFAKLSFEQKAUBJQI5G; anonauth=XYZuser%40XUMA123g; auth=demosite%40a3f7edcd509ca46e516f0249cbdc5f53; XYZdomain=www.portallink.com"
216.177.64.101 - - [09/Apr/2003:00:00:44 -0400] "GET /index.jhtml;jsessionid=OC0BPEIAAACS2CQFAKLSFEQKAUBJQI5G;jsessionid=OC0BPEIAAACS2CQFAKLSFEQKAUBJQI5G?LOGOUT=yes&DPSLogout=true&_requestid=42166 HTTP/1.0" 200 8194 "http://www.portallink.com/logout.jhtml" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" "ProfileCookie=demosite,XYZ Company, Inc.,; JSESSIONID=OC0BPEIAAACS2CQFAKLSFEQKAUBJQI5G; anonauth=XYZuser%40XUMA123g; auth=demosite%40a3f7edcd509ca46e516f0249cbdc5f53; XYZdomain=www.portallink.com"
216.177.64.101 - - [09/Apr/2003:00:00:45 -0400] "GET /index.jhtml HTTP/1.0" 200 19243 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" "ProfileCookie=demosite,XYZ Company, Inc.,; JSESSIONID=OC0BPEIAAACS2CQFAKLSFEQKAUBJQI5G; anonauth=XYZuser%40XUMA123g; auth=demosite%40a3f7edcd509ca46e516f0249cbdc5f53; XYZdomain=www.portallink.com"

Output from the script
./parse.pl sample
216.177.64.101 - - 09/Apr/2003:00:00:41 -0400 GET /logout.jhtml HTTP/1.0
216.177.64.101 - - 09/Apr/2003:00:00:44 -0400 GET /index.jhtml;jsessionid=OC0BPEIAAACS2CQFAKLSFEQKAUBJQI5G;jsessionid=OC0BPEIAAACS2CQFAKLSFEQKAUBJQI5G?LOGOUT=yes&DPSLogout=true&_requestid=42166 HTTP/1.0
216.177.64.101 - - 09/Apr/2003:00:00:45 -0400 GET /index.jhtml HTTP/1.0

Thanks in advance
what is passat
6 REPLIES 6
Ken Penland_1
Trusted Contributor

Re: Perl script

I do pretty much what you are talking about...I have a webpage that shows the contents of the access_log, filters out some stuff I dont want to see, and gives each line a radio button so I can remove everything older than it once I have reviewed it....the contents in my page is ip, user date/time method, and url.....it looks nothing like your perl script, but hopefully it will give you an idea or two...

'
Ken Penland_1
Trusted Contributor

Re: Perl script

thats wierd...I dont have a reply button, but if I hit return in the attachment field, it submits...and my attachment opens up through explorer as a webpage instead of as a text file....well, hope you can still get it ;)
'
Caesar_3
Esteemed Contributor

Re: Perl script

Hello!

Something like \"http\:\/\/(.*?)\"\s

Caesar
robert_177
Occasional Contributor

Re: Perl script

Ken,
Could you please send that script to rgoud@yahoo.com.
Thanks
what is passat
Donny Jekels
Respected Contributor

Re: Perl script

use FileHandle;

$input_file = $ARGV[0];

chomp($input_file);

$INFILE = new FileHandle;

$INFILE->open ($input_file, "r");


while ( !$INFILE->eof ) {

$line = $INFILE->getline;

if($line =~ /http\:\/\/www.portalink.com/) {

$name_flag=1;

while($name_flag && !$INFILE->eof) {
{




that is a start for a multiple seach inside a paragraph.

also if you want to search for diffrent lines
continue with

} elsif { ($line =~ /what ever/) {
bla bla bla

}


have fun.......

donny
"Vision, is the art of seeing the invisible"
Donny Jekels
Respected Contributor

Re: Perl script

hey sorry the else if is incorrect

do something liek this.

if ($line =~ /http\:\/\/www.portalink/ {
do-something
} eslif ($line =~ /sessionid/) {
do-anotherthing
}

that ends the if statement, however you can have endless elsif's

the power of perl

peace
"Vision, is the art of seeing the invisible"