- 论坛徽章:
- 0
|
本帖最后由 下后下前拳 于 2010-09-13 18:25 编辑
回复 1# 下后下前拳
这是小弟用的代码,最后插库。。现在不知道如何每次都取出新的行插进去。
use strict;
use DBI;
my @last1=`last`;
my @last=reverse(@last1);
my @split;
my $host;
my @eth0;
my $ip;
my $access_time;
my $left_time;
my $date;
my $sth;
my $dbh;
my $sql;
my $db;
open(ETH0,"/etc/sysconfig/network-scripts/ifcfg-eth0");
while(<ETH0>){
if ($_=~/IPADDR/){
@eth0=split("=",$_);
$host=$eth0[1];
}
}
close ETH0;
foreach(@last){
if($_=~/^root/){
@split=split(" ",$_);
$ip=$split[2];
$access_time=$split[6];
$left_time=$split[8];
$date=$split[4]." ".$split[5];
$db="DBI:mysql:syslog:192.168.31.23;user=root;password=";
$sql="insert into ip_access(host,access_ip,access_time,left_time,date) values('$host','$ip','$access_time','$left_time','$date')";
$dbh=DBI->connect($db);
$sth=$dbh->prepare($sql) or die "can't use sql statement" . DBI->errstr;
$sth->execute();
}
}
$sth->finish();
$dbh->disconnect();
print "insert data over.\n"; |
|