【发布时间】:2013-01-14 01:50:32
【问题描述】:
我的一点点代码有困难。
open ("files","$list");
while (my $sort = <files>) {
chomp $sort;
foreach my $key (sort keys %ips) {
if ($key =~ $sort) {
print "key $key\n";
my $match =qx(iptables -nL | grep $key 2>&1);
print "Match Results $match\n";
chomp $match;
my $banned = $1 if $match =~ (/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/);
print "Banned Results $banned\n";
if ($key =~ $banned) {
print "Already banned $banned\n";
} else {
system ("iptables -A INPUT -s $key -j DROP");
open my $fh, '>>', 'banned.out';
print "Match Found we need to block it $key\n";
print $fh "$key:$timestamp\n";
close $fh;
}
}
}
}
所以基本上我正在做的是打开每行 1 个地址的列表。
接下来,我将从脚本的另一部分中对我的关键变量进行排序,并将其与我的列表进行匹配,如果匹配,则继续执行 if 语句。
现在使用匹配的密钥,我需要检查它是否已经被阻止,所以我使用 qx 来执行该变量的 iptables 和 grep。如果匹配,一切都会完美运行。
如果不匹配,换句话说,我的 iptables -nL | grep $key 返回一个空白值,而不是继续执行我的 else 语句,它会“抓取”该空白值用于 $match 并继续执行。
在我的一生中,我无法弄清楚如何去除那个空白值并基本上将其显示为没有回报。
我知道有用于 iptables 等的模块,但是我必须让这个脚本尽可能通用。
【问题讨论】: