Posted By

richardkendall on 09/27/11


Tagged

ping perl win32


Versions (?)

perl ping host


 / Published in: Perl
 

URL: http://www.richardjameskendall.com

  1. # subroutine: ping_machine
  2. # accepts: hostname/ip address, timeout(ms), # of echos to send
  3. # returns an array:
  4. # 0: status: DNS_FAIL/PING_FAIL/PING_PARTIALLY_OKAY/PING_OKAY
  5. # 1: ip address of the host (v4)
  6. # 2: min delay (ms)
  7. # 3: max delay (ms)
  8. # 4: ave delay (ms)
  9. # calls the windows ping utility
  10. sub ping_machine {
  11. my @return_array;
  12. my $status;
  13. my $ip_address;
  14. my $max;
  15. my $min;
  16. my $ave;
  17. my $host = @_[0];
  18. my $timeout = @_[1];
  19. my $packets = @_[2];
  20. open(PING,"ping $host -w $timeout -4 -n $packets |");
  21. while(<PING>) {
  22. if(/could not find/) {
  23. $status = "DNS_FAIL";
  24. }
  25. if(/\[([0-9\.]+)\]/) {
  26. $ip_address = $1;
  27. }
  28. if(/Packets\: Sent \= ([0-9]+)\, Received \= ([0-9]+)\, Lost \= ([0-9]+)/) {
  29. if($1 eq $2) {
  30. $status = "PING_OKAY";
  31. } else {
  32. if($1 eq $3) {
  33. $status = "PING_FAIL";
  34. } else {
  35. $status = "PING_PARTIALLY_OKAY";
  36. }
  37. }
  38. }
  39. if(/Minimum \= ([0-9]+)ms\, Maximum \= ([0-9]+)ms\, Average \= ([0-9]+)/) {
  40. $min = $1;
  41. $max = $2;
  42. $ave = $3;
  43. }
  44. }
  45. $return_array[0] = $status;
  46. $return_array[1] = $ip_address;
  47. $return_array[2] = $min;
  48. $return_array[3] = $max;
  49. $return_array[4] = $ave;
  50. return @return_array;
  51. }

Report this snippet  

You need to login to post a comment.