禁止Perl代码的系统输出。
这段代码在功能上运行良好,直到我遇到一个无法解析的主机名,并且希望抑制无法解析的域的输出。
如果在lists.hosts文件中有一个无法解析的域,屏幕输出将包含:"ping: cannot resolve XXX.com: Unknown host“
my $ip;
open(HOSTLIST, "lists.hosts"); # Load domains
@hosts = <HOSTLIST>;
chomp($host);
foreach $host (@hosts) {
$results = `ping -c 1 $host`;
$record++;
my $pos = index($results, $find);
if (($results =~ /ttl=/) || ($results =~ /data bytes/)) {
#$count++;
chomp($host);
if (($results =~ /(?<=bytes from)(.*)(?=:)/) != 0) {
($ip) = ($results =~ /(?<=bytes from)(.*)(?=:)/);
}
elsif (($results =~ /(?<=\()(.*)(?=\))/) != 0) {
($ip) = ($results =~ /(?<=\()(.*)(?=\))/);
}
print "Record: $record Host: $host IP:$ip Status: Passed";
print "\n";
#print ("*** Record# $record: Ping Test Succeeded for Server: $host ***\n");
#print ("$results\n");
}
else {
$count++;
chomp($host);
#print ("*** Record# $record: Ping Test Failed for Server: $host ***\n");
print "Record: $record Host: $host Status: Failed\n";
#print ("$results\n");
}
}
close(HOSTLIST);
exit($errorcode);发布于 2014-06-11 02:51:44
您的ping调用需要捕获stderr:
ping -c 1 $host 2>&1而且,您没有检查open的返回,这是您应该经常检查的。最后,您应该在顶部使用use warnings;和use strict;。
https://stackoverflow.com/questions/24148682
复制相似问题