#!/usr/bin/perl -w use strict; use IO::Socket; # Debian: apt-get install libtime-hires-perl # RHEL: yum install perl-Time-HiRes use Time::HiRes qw( gettimeofday tv_interval); # Should come with Perl use Term::ANSIColor qw(:constants); my (@god, @dammit); sub usage { print "$0 \n"; exit(1); } &usage unless defined ($ARGV[1]); my $ip = $ARGV[0]; my $port = $ARGV[1]; my @ips; if ($ip =~ /[a-zA-Z]+/) { @ips = gethostbyname("$ip") or die BLINK BOLD RED "Can't resolve $ip", RESET . "\n"; @ips = map { inet_ntoa($_) } @ips[4 .. $#ips]; for (@ips) { &check($_,$port); } } else { &check($ip,$port); } sub check { our $h = $_[0]; our $p = $_[1]; our $message; sub timeout { print "UDP OpenVPN connection " . BLINK BOLD RED "timed out", RESET . " on $h:$p\n"; close($message); } eval { local $SIG{ALRM} = sub { &timeout; }; alarm 3; my $now = [gettimeofday()]; my $message = IO::Socket::INET->new(Proto=>"udp",PeerPort=>"$p",PeerAddr=>"$h") or die "$@"; $message->send("\x38\x40\x80\x2a\xfa\xdb\x9b\x67\x95\x00\x00\x00\x00\x00"); my ($datagram,$flags); $message->recv($datagram,26,$flags); if (length($datagram) == 26) { my $andagain = tv_interval($now)*1000; my $ms = sprintf("%d", $andagain); print "UDP OpenVPN is " . BOLD GREEN "UP", RESET . " on $h:$p and responded in $ms ms\n"; } else { print "UDP OpenVPN is " . BLINK BOLD RED "DOWN", RESET . " on $h:$p\n"; } alarm 0; } }