Perl脚本从crontab失败
我有一个perl脚本,可以读取防火墙访问日志,查看谁使用了特定的vpn帐户。当从命令行运行时,这可以正常工作,但从crontab运行时失败。代码在这里Perl脚本从crontab失败,perl,crontab,Perl,Crontab,我有一个perl脚本,可以读取防火墙访问日志,查看谁使用了特定的vpn帐户。当从命令行运行时,这可以正常工作,但从crontab运行时失败。代码在这里 #!/usr/bin/perl -w use strict; use warnings; use MIME::Lite::TT::HTML; my $basedir = "/var/log"; my @verdir = qw(fw_d); my $fulldir; my $configs; my $combidir; my @results;
#!/usr/bin/perl -w
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my $basedir = "/var/log";
my @verdir = qw(fw_d);
my $fulldir;
my $configs;
my $combidir;
my @results;
my @files1;
foreach $combidir (@verdir) {
$fulldir = "$basedir/$combidir";
opendir (DIR, $fulldir);
my @files = grep { $_ ne '.' && $_ ne '..' && $_ ne 'CVS' } readdir DIR;
closedir (DIR);
@files1 = sort {expand($a)cmp expand($b)}(@files);
foreach my $configs (@files1) {
my $now = time;
my @stat = stat("$fulldir/$configs");
if ($stat[9] > ($now - 2592000)) {
system("/usr/bin/less -f $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log"); }
}
}
results();
sendmailnew(\@results);
sub results{
my $input = "/usr/local/CCS/ravpn/output.log";
open my $fh, "<", "$input" or die "could not open '$input': $!";
while (<$fh>){
if ($_ =~ /(................)fw.*(Group = NETOPS_TUNNEL). (Username = .(authenticated.)/){
push (@results, "$1 $2 $3 $4<br>")
}
}
return @results
}
sub expand {
my $file=shift;
$file=~s{(\d+)}{sprintf "%04d", $1}eg; # expand all numbers to 4 digits
return $file;
}
sub sendmailnew {
my %params;
my @results = @{$_[0]};
$params{sorted} = "@results";
my %options;
$options{INCLUDE_PATH} = '/usr/local/CCS/ravpn/templates/';
my $msg = MIME::Lite::TT::HTML->new(
From => "",
#To => "",
BCC => "",
Subject => "",
Encoding => 'quoted-printable',
Template => {
text => 'test.txt.tt',
html => 'sort.html.tt',
},
TmplOptions => \%options,
TmplParams => \%params,
);
$msg->send;
system("rm -rf /usr/local/CCS/ravpn/output.log");
}
正在读取的文件要么是纯文本文件,要么是压缩文本文件.gz,因此我需要一种不用解压缩就能读取它们的方法,因此使用更少的资源
非常感谢您的帮助。请尝试替换
system("/usr/bin/less -f $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log");
与
Less不意味着在没有终端的情况下运行。在你的情况下没有必要这样做。如果您在perl内部执行此操作而不使用perl自己的函数调用系统
则更好:
if ($configs =~ /\.gz$/) {
open (F1, "gunzip -c $fulldir/$configs |");
} else {
open (F1, "$fulldir/$configs");
}
open (F2, ">> /usr/local/CCS/ravpn/output.log");
while (<F1>) {
print F2 $_ if (/NETOPS_TUNNEL/);
}
close F2;
close F1;
if($configs=~/\.gz$/){
打开(F1,“gunzip-c$fulldir/$configs |”);
}否则{
打开(F1,“$fulldir/$configs”);
}
打开(F2,“>>/usr/local/CCS/ravpn/output.log”);
而(){
打印F2$\uIf(/NETOPS\u TUNNEL/);
}
关闭F2;
关闭F1;
PS:我看到您使用/usr/local/CCS/ravpn/output.log作为临时文件。基于此脚本的功能,不需要该文件。它使您的脚本更加简单
编辑:也适用于.gz文件。您也可以使用IO::Uncompress::Gunzip,但这已经很复杂了。啊,对不起,最后一件事我需要提到的是,这些文件要么是文本,要么压缩为gz,我尝试使用更少的文件,因为它似乎能够读取压缩的文本文件……谢谢你,因为我曾经尝试过将事情复杂化。这项工作非常出色。
system("/bin/cat $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log");
if ($configs =~ /\.gz$/) {
open (F1, "gunzip -c $fulldir/$configs |");
} else {
open (F1, "$fulldir/$configs");
}
open (F2, ">> /usr/local/CCS/ravpn/output.log");
while (<F1>) {
print F2 $_ if (/NETOPS_TUNNEL/);
}
close F2;
close F1;