[英]Perl suppress backtick output with ping
抑制Perl代碼的系統輸出。
該代碼在功能上可以正常工作,直到遇到無法解析的主機名並希望禁止顯示無法解析的域的輸出為止。
如果在lists.hosts文件中存在無法解析的域,則屏幕輸出將包含:“ ping:無法解析XXX.com:未知主機”
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);
您對ping
調用需要捕獲stderr:
ping -c 1 $host 2>&1
此外,您並沒有檢查open
的返回,您應該始終這樣做。 最后,您應該使用use warnings;
並use strict;
在頂部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.