sql - How to get information from log files and add them to database table using Perl DBI sqlite -


i have assignment requires me take source ip , destination port log file , add them database table created using perl dbi sqlite. have tried write script not seem work. appreciate help. log file available @ http://fleming0.flemingc.on.ca/~chbaker/comp234-perl/sample.log

here code have far.

#!/usr/bin/perl  use strict; use warnings; use dbi;  %ip2port; $ipcount = keys %ip2port; $portcount = 0; $filename = "./sample.log"; open $log, "<", $filename or die "can't open $filename: $!"; line: while (my $line = <$log>) { ($src_id) = $line =~ m!src=([.\d]+)!gis; ($dst_port) = $line =~ m!dpt=([.\d]+)!gis; $dbh = dbi->connect(               "dbi:sqlite:dbname=test.db",      "",                               "",                               { raiseerror => 1 },          ) or die $dbi::errstr;   $dbh->do("insert probes values($src_id, $dst_port )"); $dbh->do("insert probes values(2,'$dst_port',57127)"); $sth = $dbh->prepare("select sqlite_version()"); $sth->execute();  $ver = $sth->fetch();  print @$ver; print "\n";  $sth->finish(); $dbh->disconnect(); } 

1) change regular expression:

my ($src_id) = $line =~ m!src=([\.\d]+)!g;  ($dst_port) = $line =~ m!dpt=([\d]+)!g; 

2) change sql's

$dbh->do("insert probes values('$src_id', $dst_port )"); 

update in case, it's better build sql sentences parameter binding , avoid sql-injection problems:

$dbh->do("insert probes values(?,?)", undef, $src_id, $dst_port); 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -