gpt4 book ai didi

perl - 如何使用 Net::Ping 使用 Perl one-liner ping 主机?

转载 作者:行者123 更新时间:2023-12-04 12:19:56 24 4
gpt4 key购买 nike

尝试将以下 Perl 单行程序集成到 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 alive\n"}'

或者,您可以将主机作为参数传入 - 请参阅其他答案。

关于perl - 如何使用 Net::Ping 使用 Perl one-liner ping 主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3960595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com