Categories
Computers Linux Mac

Ping with timestamp

For some reason standard ICMP utilities (e.g. ‘ping’) do not support formatting the output in a way that makes it easy to do further processing. The following Perl script should work on any Mac or Linux system. It wraps the standard ping command and formats the output as comma separated values that can be directly imported into for instance Microsoft Excel.


#!/usr/bin/perl

use strict;
$| = 1;

my $host = $ARGV[0];
open PING, "ping $host |" or die "Error :$!";

while() {
 if($_ =~ m/.*seq=(\d+).*time=([\.0-9]*).*/) {
  my($s,$m,$h,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  my $dt = sprintf("%4d-%02d-%02d,%02d:%02d:%02d", $year+1900, $mon+1, $mday, $h, $m, $s);
  print "$dt,$1,$2\n"
 }
}

Download script.

One reply on “Ping with timestamp”

Martin – just what I was looking for. Modified the ping command to make it just ping every minute (-i 60) and it is perfect – thanks.

Comments are closed.

css.php