加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用Net :: Ping使用Perl单线程来ping主机?

发布时间:2020-12-15 21:46:34 所属栏目:大数据 来源:网络整理
导读:尝试将以下Perl one-liner集成到 shell脚本中.此代码在Perl脚本中工作,但不是作为从shell脚本执行的一行代码. 我试过用真正的主机名取代$host而没有运气. #!/bin/kshhosts="host1 host2 host3"PERL=/usr/bin/perl# Check to see if hosts are accessible.for
尝试将以下Perl one-liner集成到 shell脚本中.此代码在Perl脚本中工作,但不是作为从shell脚本执行的一行代码.

我试过用真正的主机名取代$host而没有运气.

#!/bin/ksh

hosts="host1 host2 host3"

PERL=/usr/bin/perl

# Check to see if hosts are accessible.
for host in $hosts
do
   #echo $host
   $PERL -e 'use Net::Ping; $timeout=5; $p=Net::Ping->new("icmp",$timeout) or die bye ; print "$host is alive n" if $p->ping($host); $p->close;'
done

解决方法

shell中的单引号会阻止$host被解释.因此,您可以根据需要停止并重新启动单引号:
perl -MNet::Ping -e 'if (Net::Ping->new("icmp",5)->ping("'$host'")) {print "'$host' is aliven"}'

或者,您可以将主机作为参数传递 – 请参阅其他答案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读