check_netdns.pl 2.9 KB

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