【发布时间】:2014-06-10 18:48:03
【问题描述】:
抑制 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);
【问题讨论】:
-
请始终
use strict和use warnings在您编写的每个 Perl 程序的开头。这尤其适用于您寻求代码帮助时。您还应该正确缩进代码以使其可读。在这种情况下,我已经为您完成了,但您应该自己完成,因为它可以帮助您查看代码的流程并更容易发现任何错误
标签: perl system output suppress