我已经编写了以下代码来连续记录带有时间戳的 ping RTT 服务器。脚本是命令行中的工作文件但是我的痛苦日志文件 Ping_10.112.114.11.txt 是空的,我在代码中看不到任何错误或警告或语法错误。
下面是我的代码:
#!/user/bin/perl
use Net::Ping;
use Time::HiRes;
use strict;
open (LOG , ">Ping_10.112.114.11.txt") || die "Cannot create log file :$!";
print LOG "File is ready to write\n\n";
my $host ="10.112.114.11";
my $p = Net::Ping->new(); #tcp echo request for ping check
$p->hires();
do
{ my ($ret, $duration, $ip) = $p->ping($host);
my $time = localtime;
my $dur = (int(1000*$duration))/1000;
print "$time\t$host is alive (packet RTT: $dur ms)\n";
print LOG "$time\t$host is alive (packet RTT: $dur ms)\n";
} while(1);
#END
我还尝试将输出保存到一个变量,然后将其发送到文件句柄进行打印,但不幸的是,我的努力似乎都没有正常工作。
my $output = ""$time\t$host is alive (packet RTT: $dur ms)\n";
print LOG "$output";
任何人都可以帮助我突出我的愚蠢错误,因为我编写了许多使用相同 perl 语法但工作正常的脚本。
我的总体要求是编写一个脚本,该脚本运行以在日志文件中连续记录服务器的 RTT 和连接性,并带有时间戳。就像 Ping -t 的输出带有额外的时间戳值一样。
提前致谢 :)