Created
November 24, 2022 18:04
-
-
Save yaasita/91f320ce210005c69125b10ee6095736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use feature qw(:5.10); | |
use utf8; | |
use List::Util; | |
# ipはダミー | |
my $WEBARENA_IPV4 = "203.0.113.1"; | |
my $WEBARENA_IPV6 = "2001:db8::1"; | |
my $ORACLE1_IPV4 = "203.0.113.1"; | |
my $ORACLE2_IPV4 = "203.0.113.2"; | |
my $RESULT_FILE = "./out/ping.csv"; | |
my @result; | |
sub ping { | |
my $host = shift; | |
my $cmd; | |
if ($host =~ /:/){ | |
$cmd = "ping6"; | |
} | |
else { | |
$cmd = "ping"; | |
} | |
my $output = `$cmd -c 10 $host`; | |
say $output; | |
my $result = { | |
time => time(), | |
host => $host | |
}; | |
if ($output =~ /^rtt \S+ = (\S+) ms/m) { | |
my @rtt = split(/\//, $1); | |
$result->{rtt} = $rtt[1]; | |
} | |
else { | |
die "output format error"; | |
} | |
if ($output =~ /(\d+)% packet loss/){ | |
$result->{loss} = $1; | |
} | |
else { | |
die "output format error"; | |
} | |
push(@result, $result); | |
} | |
my @hosts = List::Util::shuffle( | |
$WEBARENA_IPV4 | |
,$WEBARENA_IPV6 | |
,$ORACLE1_IPV4 | |
,$ORACLE2_IPV4 | |
); | |
ping($_) for @hosts; | |
{ | |
open (my $wr,">>:utf8", $RESULT_FILE) or die $!; | |
my @order = qw(time host rtt loss); | |
say $wr join(",", @order) if -z $RESULT_FILE; | |
for my $r (@result){ | |
my @line; | |
push(@line, $r->{$_}) for @order; | |
say $wr join(",", @line); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment