summaryrefslogtreecommitdiff
path: root/scripts/network/ping_log
blob: 878127b2f773c834943e24659a31797cba9e2166 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/perl -w

use POSIX;
use IO::Handle;

@targets = ("4.2.2.2", "8.8.8.8", "10.132.128.1");

open $_, "-|", "ping", $_ for @targets;
-d or mkdir for "$ENV{HOME}/tmp";
open LOG, ">>", "$ENV{HOME}/tmp/ping_log.txt";
LOG->autoflush;
while (1) {
    $to_print = "";
    for (@targets) {
	$rin = "";
	vec($rin, fileno $_, 1) = 1;
	next unless select $rin, undef, undef, 1;
	$line = readline $_;
	if ($line =~ /bytes from/ and not $status{$_}) {
	    $status{$_}++;
	    $to_print .= "$_ up, ";
	} elsif ($line =~ /Unreachable/ and $status{$_}) {
	    $status{$_}--;
	    $to_print .= "$_ down, ";
	}
    }
    next unless length $to_print;
    $to_print = strftime("%c", localtime) . " " . substr $to_print, 0, -2;
    print "$to_print\n";
    print LOG "$to_print\n";
}