Last active
December 11, 2015 11:51
-
-
Save Kilobyte22/15eedd7b08c59d70d274 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/perl | |
# i3blocks script to display current ping | |
# Licensed under the MIT license | |
use strict; | |
use warnings; | |
sub ping { | |
my $host = shift; | |
my $data = `ping '$host' -c 4 2>&1`; | |
my @lines = split /\n/, $data; | |
if ((shift @lines) =~ /unreachable/) { | |
my %ret = ('reachable' => 0); | |
return %ret; | |
} else { | |
my $times = pop @lines; | |
my $stats = pop @lines; | |
$stats =~ /(\d+)%/; | |
my $percent = $1; | |
$times =~ /((?:[0-9\.]+\/){3}[0-9\.]+)/; | |
my @fields = split /\//, $1; | |
my $avg = $fields[1]; | |
my $jitter = $fields[3]; | |
my %ret = ('avg' => $avg, 'jitter' => $jitter, 'percent' => $percent, 'reachable' => 1); | |
return %ret; | |
} | |
} | |
my %data = ping '8.8.8.8'; | |
if ($data{reachable}) { | |
print "$data{avg}ms ±$data{jitter}ms"; | |
print ", $data{percent}% loss" if ($data{percent} > 0); | |
print "\n$data{avg}ms\n"; | |
if ($data{percent} > 0 || $data{avg} > 100) { | |
print '#FF0000'; | |
} elsif ($data{avg} > 50) { | |
print '#FFFF00'; | |
} | |
} else { | |
print "Network Unreachable\nNONET"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment