首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl使用ping禁止反引号输出

问Perl使用ping禁止反引号输出
EN

Stack Overflow用户
提问于 2014-06-11 02:48:03
回答 1查看 360关注 0票数 0

禁止Perl代码的系统输出。

这段代码在功能上运行良好,直到我遇到一个无法解析的主机名,并且希望抑制无法解析的域的输出。

如果在lists.hosts文件中有一个无法解析的域,屏幕输出将包含:"ping: cannot resolve XXX.com: Unknown host“

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2014-06-11 02:51:44

您的ping调用需要捕获stderr:

代码语言:javascript
复制
ping -c 1 $host 2>&1

而且,您没有检查open的返回,这是您应该经常检查的。最后,您应该在顶部使用use warnings;和use strict;。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24148682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档