Skip to content

Instantly share code, notes, and snippets.

@yeahnoob
Last active May 1, 2019 01:37
Show Gist options
  • Save yeahnoob/ede52dd6406c9d04c544 to your computer and use it in GitHub Desktop.
Save yeahnoob/ede52dd6406c9d04c544 to your computer and use it in GitHub Desktop.
Ping & Log
#!/usr/bin/perl
use Net::Ping;
use Time::HiRes;
$host = $ARGV[0];
while (1) {
open $fd, '>>:encoding(UTF-8)', 'ping.pl.log';
$p = Net::Ping->new("icmp");
$p->hires();
($ret, $duration, $ip) = $p->ping($host, 2); #config ping timeout=2sec.
print $fd scalar(localtime);
print $fd "\n";
print $fd sprintf("$host [ip: $ip] is alive (delay: %.2f ms)\n", 1000 * $duration) if $ret;
$p->close();
close $fd;
sleep 1; #waiting time between two "ping"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment