#!/usr/bin/perl ### ### XMLPING.PL ### (c) 2003 Software Poetry, Inc. ### http://www.softwarepoetry.com/webob ### ### Really a stupid way to do this, but everybody makes it awfully hard ### to actually ping stuff. The bastards. ### ### This code is built to work with Windows or our Linux ping... it could ### easily be converted for other systems, but you'll probably have to screw ### around with the ping command-line format and regexp for extracting ### values. ### ### License: This software is provided as-is with NO WARRANTY WHATSOEVER. ### Software Poetry makes no claims as to its fitness for any purpose. ### This file and any derivatives or translations of it may be freely ### copied and redistributed so long as the original license and copyright ### notices are included and not altered. ### use strict; use Config; # +------------------------------------------------------------------------- # | Configuration Variables # +------------------------------------------------------------------------- my $c_cPingDefault = 5; # +------------------------------------------------------------------------- # | Entrypoint # +------------------------------------------------------------------------- my $cPing; my $szPingOutput; my $szHost; my $szAddr; my $pctLoss; my $msMin; my $msMax; my $msAvg; if ($#ARGV < 0) { print "Usage: perl xmlping.pl host [packet-count]"; exit(1); } if ($#ARGV < 1) { $cPing = $c_cPingDefault; } else { $cPing = @ARGV[1] } if ($Config{'osname'} eq 'MSWin32') { my $szPingOutput = `ping -n $cPing @ARGV[0]`; $szPingOutput =~ /Pinging ([^ ]+) .*statistics for ([^\:]+):.*([0-9]+)%.*Minimum = ([0-9\.]+)ms, Maximum = ([0-9\.]+)ms, Average = ([0-9\.]+)ms/s; $szHost = $1; $szAddr = $2; $pctLoss = $3; $msMin = $4; $msMax = $5; $msAvg = $6; } else { my $szPingOutput = `ping -c $cPing @ARGV[0]`; $szPingOutput =~ /PING ([^ ]+) \(([^\)]+).* ([0-9]+)%.* ([0-9\.]+)\/([0-9\.]+)\/([0-9\.]+)\//s; $szHost = $1; $szAddr = $2; $pctLoss = $3; $msMin = $4; $msMax = $6; $msAvg = $5; } if ($szHost eq "") { $szHost = @ARGV[0]; } if ($pctLoss eq "") { $pctLoss = "100"; } if ($msMin eq "") { $msMin = 0; } if ($msMax eq "") { $msMax = 0; } if ($msAvg eq "") { $msAvg = 0; } print "\n"; print "\t$szHost\n"; print "\t$szAddr\n"; print "\t$pctLoss\n"; print "\t$msMin\n"; print "\t$msMax\n"; print "\t$msAvg\n"; print "\n"; if ($pctLoss == 100) { exit(1); } exit(0);