- 论坛徽章:
- 0
|
#!/bin/perl
use IO::Socket;
use threads;
use threads::shared;
use DBI();
use strict;
#---------------------------------------------------------
#DB connection
my ($url,$user,$passwd)=("DBI:mysql:database=logserver;port=3306;host=127.0.0.1","root","123qwe");
our $dbh=DBI->connect($url,$user,$passwd) or die "can't connect DB mysql ".DBI->errstr;
my $ipHead="192.168.4.";
my (@ips,@iplist):shared;
our @logtype:shared=('test');
for(my $i=1;$i<255;$i++){push(@ips,$ipHead."$i");}
my @threads;
print localtime."\n";
for(my $j=0;$j<@ips;$j++){$threads[$j]=threads->new(\&portScaner,$ips[$j]);}
foreach my $threadinstance(@threads){$threadinstance->join();}
print localtime."\n";
#---------------------------------------------------------
#scan if the port 22 is open
sub portScaner{
my ($ip)=(shift);
my $result=IO::Socket::INET->new("$ip:22");
if($result){
push(@iplist,$ip);
foreach my $type(@logtype){&getLogtypeContext($type);}
$result->close;
}
#else {writeLog("ip=$ip port 22 is not open!\n");}
}
#---------------------------------------------------------
#get filepath keywords splitwords in logtype
sub getLogtypeContext{
my ($logtype)=@_;
my $sth=$dbh->prepare("select filepath,keyword,splitword from t_logtype where type='$logtype';") or die $dbh->errstr;
$sth->execute();
my (@filepath,@keyword,@splitword);
while(my $ref=$sth->fetchrow_hashref())
{
push(@filepath,split(",",$ref->{filepath}));
push(@keyword,split(",",$ref->{keyword}));
push(@splitword,split(",",$ref->{splitword}));
}
return (\@filepath,\@keyword,\@splitword);
}
我现在想在多线程中使用DBI查询,dbh怎么设置shared呢,我现在这个提示无法shared?谢谢
回复 4# shijiang1130
|
|