4
0

check_netdns.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/perl -w
  2. # Perl version of check_dns plugin which calls DNS directly instead of
  3. # relying on nslookup (which has bugs)
  4. #
  5. # Copyright 2000, virCIO, LLP
  6. #
  7. # $Log$
  8. # Revision 1.3 2002/05/07 05:35:49 sghosh
  9. # 2nd fix for ePN
  10. #
  11. # Revision 1.2 2002/05/02 16:43:29 sghosh
  12. # fix for embedded perl
  13. #
  14. # Revision 1.1.1.1 2002/02/28 06:43:00 egalstad
  15. # Initial import of existing plugin code
  16. #
  17. # Revision 1.1 2000/08/03 20:41:12 karldebisschop
  18. # rename to avoid conflict when installing
  19. #
  20. # Revision 1.1 2000/08/03 19:27:08 karldebisschop
  21. # use Net::DNS to check name server
  22. #
  23. # Revision 1.1 2000/07/20 19:09:13 cwg
  24. # All the pieces needed to use my version of check_dns.
  25. #
  26. #
  27. use Getopt::Long;
  28. use Net::DNS;
  29. use lib utils.pm;
  30. use utils ;
  31. my $PROGNAME = "check_netdns";
  32. Getopt::Long::Configure(`bundling`);
  33. GetOptions("V" => $opt_V, "version" => $opt_V,
  34. "h" => $opt_h, "help" => $opt_h,
  35. "t=i" => $opt_t, "timeout=i" => $opt_t,
  36. "s=s" => $opt_s, "server=s" => $opt_s,
  37. "H=s" => $opt_H, "hostname=s" => $opt_H);
  38. # -h means display verbose help screen
  39. if($opt_h){ print_help(); exit 0; }
  40. # -V means display version number
  41. if ($opt_V) { print_version(); exit 0; }
  42. # -H means host name
  43. $opt_H = shift unless ($opt_H);
  44. unless ($opt_H) { print_usage(); exit -1; }
  45. if ($opt_H &&
  46. $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
  47. {
  48. $host = $1;
  49. } else {
  50. print "$opt_H is not a valid host name";
  51. exit -1;
  52. }
  53. # -s means server name
  54. $opt_s = shift unless ($opt_s);
  55. if ($opt_s) {
  56. if ($opt_s =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
  57. {
  58. $server = $1;
  59. } else {
  60. print "$opt_s is not a valid host name";
  61. exit -1;
  62. }
  63. }
  64. # -t means timeout
  65. my $timeout = 10 unless ($opt_t);
  66. my $res = new Net::DNS::Resolver;
  67. #$res->debug(1);
  68. if ($server) {
  69. $res->nameservers($server);
  70. }
  71. $res->tcp_timeout($timeout);
  72. $SIG{ALRM} = &catch_alarm;
  73. alarm($timeout);
  74. $query = $res->query($host);
  75. if ($query) {
  76. my @answer = $query->answer;
  77. if (@answer) {
  78. print join(`/`, map {
  79. $_->type . ` ` . $_->rdatastr;
  80. } @answer);
  81. exit 0;
  82. } else {
  83. print "empty answer";
  84. exit 2;
  85. }
  86. }
  87. else {
  88. print "query failed: ", $res->errorstring, "";
  89. exit 2;
  90. }
  91. sub catch_alarm {
  92. print "query timed out";
  93. exit 2;
  94. }
  95. sub print_version () {
  96. my $arg0 = $0;
  97. chomp $arg0;
  98. print "$arg0 version 0.1";
  99. }
  100. sub print_help() {
  101. print_version();
  102. print "";
  103. print "Check if a nameserver can resolve a given hostname.";
  104. print "";
  105. print_usage();
  106. print "";
  107. print "-H, --hostname=HOST";
  108. print " The name or address you want to query";
  109. print "-s, --server=HOST";
  110. print " Optional DNS server you want to use for the lookup";
  111. print "-t, --timeout=INTEGER";
  112. print " Seconds before connection times out (default: 10)";
  113. print "-h, --help";
  114. print " Print detailed help";
  115. print "-V, --version";
  116. print " Print version numbers and license information";
  117. }
  118. sub print_usage () {
  119. my $arg0 = $0;
  120. chomp $arg0;
  121. print "$arg0 check_dns -H host [-s server] [-t timeout]";
  122. print "$arg0 [-h | --help]";
  123. print "$arg0 [-V | --version]";
  124. }