[英]How to get information from log files and add them to database table using Perl DBI sqlite
我有這項工作,需要我從此日志文件中獲取源IP和目標端口,並將它們添加到我使用Perl dbi sqlite創建的數據庫表中。 我試圖編寫一個腳本來做到這一點,但它似乎不起作用。 我將不勝感激任何幫助。 日志文件位於http://fleming0.flemingc.on.ca/~chbaker/COMP234-Perl/sample.log
這是我到目前為止的代碼。
#!/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();
}
1)更改您的正則表達式:
my ($src_id) = $line =~ m!SRC=([\.\d]+)!g;
my ($dst_port) = $line =~ m!DPT=([\d]+)!g;
2)更改您的SQL
$dbh->do("INSERT INTO probes VALUES('$src_id', $dst_port )");
UPDATE無論如何,最好使用參數綁定來構建sql語句,並避免SQL注入問題:
$dbh->do("INSERT INTO probes VALUES(?,?)", undef, $src_id, $dst_port);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.