#!/usr/bin/perl use strict; use warnings; use IO::Handle; print "Pinging 8.8.8.8 60 times and looking at the packet loss:\n"; while (1) { my $shell_call = "ping -c 60 8.8.8.8 -q"; open(my $fh, "$shell_call 2>&1 |") or die "failed to call: [$shell_call], error was: $!\n"; while(<$fh>) { chomp; my $line = $_; if ($line =~ / (\d+)% packet loss/) { my $packet_loss = $1; my $date = get_date(); print "$date; Packet loss: [$packet_loss %]\n"; } else { #print "line: [$line]\n"; } } close $fh; } sub get_date { my ($time) = @_; $time = time if not defined $time; my @time = localtime($time); my $year = ($time[5] + 1900); my $month = sprintf("%.2d", ($time[4] + 1)); my $day = sprintf("%.2d", $time[3]); my $hour = sprintf("%.2d", $time[2]); my $minute = sprintf("%.2d", $time[1]); my $second = sprintf("%.2d", $time[0]); # this returns "yyyy-mm-dd_hh:mm:ss". my $date = "$year-$month-$day $hour:$minute:$second"; return ($date); }