Подскажите плз, вот есть такой скрипт:#!/usr/bin/perl
open (OUTPUT, '>>/var/www/tooff.html') || die print "Can't open file $basename: $!";
#print OUTPUT "Content-type: text/html\n\n";
$during=30; # time of sniffing in seconds
$count=10; # count of packets
if (!defined($kidpid = fork())) {
die "Cannot fork: $!";
} elsif ($kidpid == 0) {
print("$kidpid");
#exec("rm virus-out");
exec("tcpdump -i fxp0 | grep netbios-ns >> virus-out");
die "Can't exec tcpdump: $!";
print("$kidpid");
} else {
sleep ($during);
kill ($kidpid);
}
#system('tcpdump -i eth0 | grep netnios-ns >> virus-out');open F, './virus-out';
while (<F>) {
@t = split( /\s/ );
if ( $t[1] =~ /192\.168\.1\.(.*?)\..*netbios-ns$/ ) {
$c{$1} += 1;
};};
close F;
&print_top;
print OUTPUT "At the next computers were finded viruses:";
print OUTPUT "<b>IP address:\t\t\t COUNT</b><br>";
foreach $k (sort( keys(%c)))
{
if ($c{$k}>$count)
{
print OUTPUT "<tr>";
print OUTPUT "IP - 192.168.1.".$k."\t<b>COUNT - ".$c{$k}."\n</b><br>";
print OUTPUT "</tr>";
};
}
&print_bottom;
# close outputfile
close OUTPUT;sub print_top {
# print << "[TOP]";
print OUTPUT "<html>";
print OUTPUT "<head>";
print OUTPUT "<meta http-equiv=\"Content-Language\" content=\"en-us\">";
print OUTPUT "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">";
print OUTPUT "<title>Warning!</title>";
print OUTPUT "</head>";
print OUTPUT "<body>";
print OUTPUT "<table border=\"0\" width=\"100%\">";
}sub print_bottom {
print OUTPUT "</table>";
print OUTPUT "</body>";
print OUTPUT "</html>";Надо чтобы запускался tcpdump, потом через время $during убивался....Скрипт работает, но tcpdump остается висеть процессом.
Не красиво все это, не большой я спец в perl, но рискну все же подкинуть идею: надо бы заменить конструкцию$kidpid = fork();
if ($kidpid == 0) {
...
}
else {
sleep ($during);
kill ($kidpid); #Здесь явно после убийства процесса с pid=$kidpid
#бесхозный tcpdump подхватывается init и продолжает
#работу.
}на
$kidpid = fork();
if ($kidpid == 0) {
...
sleep ($during);
exit 0;
}
else {
wait(); #не знаю, есть ли такая на perl и нужна ли она здесь вообще
}Не знаю, улучшит ли это ситуацию или нет, зато покрасивше выглядит. А вот в мануале предлагают совсем правильный пример:
open(STATUS, "netstat -an 2>&1 |") || die "can't fork: $!";
while (<STATUS>) {
next if /^(tcp|udp)/;
print;
}
close STATUS || die "bad netstat: $! $?";это уж точно получше будет, чем вызов exec(), или system().
вот мой вариант скрипта#!/usr/bin/perl
use POSIX;
$tcpdump='/usr/sbin/tcpdump';
$runfile='/var/run/viruscatch';
$logfile='/tmp/tcpdump';
$clientfile='/home/httpd/html/billing/virusclients';sub onexit {
unlink $runfile;
exit 0;
}sub onexit2 {
if($childpid>0) {
kill 15,$childpid;
}
}
$SIG{INT} = \&onexit;
$SIG{TERM} = \&onexit;
$SIG{CHILD} = \&onexit;if( -e $runfile) {
if(!open(FILE,"<$runfile")) {
print "Cant't open existing pidfile $runfile! Aborting...\n";
exit 1;
}
$oldpid=<FILE>;
close FILE;
$cnt=kill 0,$oldpid;
print "$oldpid=$cnt \n";
if($cnt==0) {
print "Remove stale lock $runfile\n";
unlink $runfile;
}
else {
print "Another process already running! Aborting...\n";
exit 1;
}}
if(!open(FILE,">$runfile")) {
print "Can't open pidfile for write. Aborting...\n";
exit 1;
}
print FILE $$;
close FILE;$childpid=fork();
if(!defined($childpid)) {
print "Can't fork!\n";
onexit();
}if($childpid!=0) {
#parent process
$SIG{INT}=\&onexit2;
$SIG{KILL}=\&onexit2;
$SIG{ALRM}=\&onexit2;
alarm 15;
wait();
}
else {
#child process
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT,">$logfile" or die "Can't write to $logfile: $!";
open STDERR,'>/dev/null' or die "Can't write to /dev/null: $!";
@args=("-c","2048","-i","eth0","-n","-q","dst","port","445");
exec $tcpdump, @args;
}
$currtime=time();if(open(FILE,"<$logfile")) {
while(<FILE>) {
@a=split /\s+/;
$a[1]=~/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
$ips{$1}+=1;
}
close FILE;
if(open(FILE,"<$clientfile")) {
while(<FILE>) {
chomp;
@a=split /\s+/;
# print $a[0]," ",$a[1],"\n";
if($a[1]+86400 > $currtime && $a[0] ne '') {
$clients{$a[0]}=$a[1];
}
}
close FILE;
}
for $key (keys %ips) {
if($ips{$key}>5) { $clients{$key}=$currtime; }
}if(open(FILE,">$clientfile")) {
for $key (keys %clients) {
print FILE $key," ",$clients{$key},"\n";
}
close FILE;
}
}
onexit();
>вот мой вариант скриптаОгромное спасибо!