#!/usr/bin/env perl use strict; use warnings; use 5.010; no if $] >= 5.018, warnings => "experimental::smartmatch"; use File::ShareDir qw(dist_file); use Getopt::Long qw(:config bundling); use IO::Handle; use IPC::Run qw(harness); use SDL qw(:init); use SDL::Mixer; use SDL::Mixer::Music; use Term::Size; use Time::HiRes qw(usleep); our $VERSION = '1.32'; my $beep = 0; my $beep_when = 'both'; my $id = 0; my $rtt = 0; my $last_id = 0; my $dead_count = 0; my $column = 0; my $column_max = Term::Size::chars; my $volume = 100; my $beep_file; my $quiet = 0; my $timeout = 3; GetOptions( 'b|beep-when=s' => \$beep_when, 'f|beep-file=s' => \$beep_file, 'l|volume=f' => \$volume, 'q|quiet' => \$quiet, 't|timeout=f' => \$timeout, 'V|version' => sub { say "ekgping version ${VERSION}"; exit 0 }, ) or usage(); $beep_file //= dist_file( 'ekgping', 'beep.ogg' ); if ( $volume < 0 or $volume > 100 ) { warn("-l / --volume: Argument must be between 0 and 100. Setting to 50.\n"); $volume = 50; } if ( not $quiet ) { if ( SDL::init(SDL_INIT_AUDIO) != 0 ) { warn("Failed to init SDL\n"); } if ( SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 ) != 0 ) { warn("Failed to open audio device AUDIO_S16SYS"); } } my $host = shift or usage(); my @ping_opts = @ARGV; my $beep_sound = SDL::Mixer::Music::load_MUS($beep_file); my $ping = harness( [ 'ping', '-n', @ping_opts, $host ], '<' => \undef, '>&' => \&parse_ping_output, ); local $SIG{INT} = \&quit; local $SIG{TERM} = \&quit; local $SIG{QUIT} = \&quit; sub parse_ping_output { my ($line) = @_; chomp($line); if ( $line =~ m{ ^ \d+ \s bytes \s from \s \S+ \s icmp_.eq = (? \d+ ) \s ttl = (? \d+ ) \s time = (?