How to load SQLite3 database with log file entries using perl -


i have assignment in perl class contains 2 programs, first create database , table using dbi::sqlite perl, have completed first part of program, have no idea how go doing second part. second part involves getting source ip , destination port pairs log file , inserting them table using sql insert statement. table created in first program contains 2 columns, 1 source ip , 1 destination ports, program shouldn't produce output except count of rows added table. can see below code have put represents how table setup, appreciate on learning how fill table. log file needed second program available @ link: http://fleming0.flemingc.on.ca/~chbaker/comp234-perl/sample.log

the code first program

#!/usr/bin/perl  use strict; use warnings; use dbi;  $dbh = dbi->connect(               "dbi:sqlite:dbname=test.db",      "",     "",     { raiseerror => 1} ) or die $dbi::errstr; $dbh->do(<<'end_sql'); create table probes (     source char(15) not null,     port char(5) not null,     primary key (source, port) ) end_sql 

code second program (not working out)

#!/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(); } 

read files line line (dozens of examples in net).

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

then insert data database. (dozens of examples in net).

some reading: http://www.perl.com/pub/1999/10/dbi.html


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -