[英]How to locate an sqlite database using the sqlite3 command shell in mac?
[英]How to load SQLite3 database with log file entries using perl
我在包含兩個程序的Perl類中有一個作業,第一個是使用DBI :: SQLite for Perl創建數據庫和表,我已經完成了程序的第一部分,但是我不知道該怎么做關於第二部分。 第二部分涉及從日志文件獲取源IP和目標端口對,並使用SQL insert語句將它們插入表中。 在第一個程序中創建的表包含兩列,一列用於源IP,一列用於目標PORTS,該程序除了產生添加到表中的行數外,不應產生任何輸出。 如您在下面看到的,我放置的代碼代表如何設置表格,我將非常感謝您學習如何填充表格的幫助。 可通過以下鏈接獲得第二個程序所需的日志文件: http : //fleming0.flemingc.on.ca/~chbaker/COMP234-Perl/sample.log
我的第一個程序的代碼
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $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
第二個程序的代碼(無法解決)
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my %ip2port;
my $IPCount = keys %ip2port;
my $portCount = 0;
my $filename = "./sample.log";
open my $LOG, "<", $filename or die "Can't open $filename: $!";
LINE: while (my $line = <$LOG>) {
my ($src_id) = $line =~ m!SRC=([.\d]+)!gis; my ($dst_port) = $line =~ m!DPT=([.\d]+)!gis;
my $dbh = DBI->connect(
"dbi:SQLite:dbname=test.db",
"",
"",
{ RaiseError => 1 },
) or die $DBI::errstr;
$dbh->do("INSERT INTO probes VALUES($src_id, $dst_port )");
$dbh->do("INSERT INTO probes VALUES(2,'$dst_port',57127)");
my $sth = $dbh->prepare("SELECT SQLITE_VERSION()");
$sth->execute();
my $ver = $sth->fetch();
print @$ver;
print "\n";
$sth->finish();
$dbh->disconnect();
}
逐行閱讀文件(網絡中有數十個示例)。
my ($src_id) = $line =~ m!SRC=([\.\d]+)!gis;
my ($dst_port) = $line =~ m!DPT=([\.\d]+)!gis;
然后將此數據插入數據庫。 (網絡中有數十個示例)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.