[22972] | 1 | #!/usr/bin/perl |
---|
[17682] | 2 | # $Id: hostinfo.pl,v 1.5 2002-06-11 21:12:59 jweiss Exp $ |
---|
[13429] | 3 | |
---|
[13765] | 4 | # Copyright 1998 by the Massachusetts Institute of Technology. |
---|
| 5 | # |
---|
| 6 | # Permission to use, copy, modify, and distribute this |
---|
| 7 | # software and its documentation for any purpose and without |
---|
| 8 | # fee is hereby granted, provided that the above copyright |
---|
| 9 | # notice appear in all copies and that both that copyright |
---|
| 10 | # notice and this permission notice appear in supporting |
---|
| 11 | # documentation, and that the name of M.I.T. not be used in |
---|
| 12 | # advertising or publicity pertaining to distribution of the |
---|
| 13 | # software without specific, written prior permission. |
---|
| 14 | # M.I.T. makes no representations about the suitability of |
---|
| 15 | # this software for any purpose. It is provided "as is" |
---|
| 16 | # without express or implied warranty. |
---|
| 17 | |
---|
| 18 | # This is a simple DNS querying program. It was originally written in |
---|
| 19 | # C, but has been rewritten as a wrapper around "host" in the BIND |
---|
| 20 | # distribution. |
---|
| 21 | |
---|
[22972] | 22 | $hostprog = "/usr/bin/host"; |
---|
[13429] | 23 | |
---|
| 24 | $long_output = $show_host = $show_addr = $show_hinfo = $show_mx = 1; |
---|
[22971] | 25 | $server = ""; |
---|
[13429] | 26 | |
---|
[13516] | 27 | sub usage { |
---|
| 28 | print STDERR "Usage: hostinfo <options> <host-names-or-addresses>\n"; |
---|
| 29 | print STDERR " -h: output only hostname\n"; |
---|
| 30 | print STDERR " -a: output only address\n"; |
---|
| 31 | print STDERR " -i: output only host info record\n"; |
---|
| 32 | print STDERR " -m: output only mx record\n"; |
---|
| 33 | print STDERR " -q: disable additional query for hinfo & mx\n"; |
---|
| 34 | print STDERR " -ns <server>: ask specified name server\n"; |
---|
| 35 | exit 1; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | if (! @ARGV) { |
---|
| 39 | &usage(); |
---|
| 40 | } |
---|
| 41 | |
---|
[13429] | 42 | while (@ARGV) { |
---|
| 43 | my $arg = shift(@ARGV); |
---|
| 44 | if ($arg eq "-h") { |
---|
| 45 | $show_host = 1; |
---|
| 46 | $long_output = $show_addr = $show_hinfo = $show_mx = 0; |
---|
| 47 | next; |
---|
| 48 | } elsif ($arg eq "-a") { |
---|
| 49 | $show_addr = 1; |
---|
| 50 | $long_output = $show_host = $show_hinfo = $show_mx = 0; |
---|
| 51 | next; |
---|
| 52 | } elsif ($arg eq "-i") { |
---|
| 53 | $show_hinfo = 1; |
---|
| 54 | $long_output = $show_host = $show_addr = $show_mx = 0; |
---|
| 55 | next; |
---|
| 56 | } elsif ($arg eq "-m") { |
---|
| 57 | $show_mx = 1; |
---|
| 58 | $long_output = $show_host = $show_addr = $show_hinfo = 0; |
---|
| 59 | next; |
---|
| 60 | } elsif ($arg eq "-q") { |
---|
| 61 | $long_output = $show_host = $show_addr = 1; |
---|
| 62 | $show_hinfo = $show_mx = 0; |
---|
| 63 | next; |
---|
| 64 | } elsif ($arg eq "-ns") { |
---|
[22971] | 65 | $server = shift(@ARGV) || &usage(); |
---|
| 66 | if ($server =~ /[^a-zA-Z0-9.-]/) { |
---|
| 67 | print STDERR "Bad hostname '$server'.\n"; |
---|
| 68 | &usage(); |
---|
[13429] | 69 | } |
---|
| 70 | system "$hostprog -t a $server > /dev/null 2>&1"; |
---|
| 71 | if ($?) { |
---|
| 72 | print STDERR "No such host '$server'.\n\n"; |
---|
[22971] | 73 | $server = ""; |
---|
[13429] | 74 | } |
---|
| 75 | next; |
---|
| 76 | } elsif ($arg =~ /^-/) { |
---|
[13516] | 77 | &usage(); |
---|
[13429] | 78 | } |
---|
| 79 | |
---|
| 80 | if ($arg =~ /[^a-zA-Z0-9.-]/) { |
---|
| 81 | print STDERR "Bad hostname '$arg'.\n"; |
---|
| 82 | next; |
---|
| 83 | } |
---|
| 84 | |
---|
[15351] | 85 | $host = $hinfo = ""; |
---|
| 86 | @addr = @mx = (); |
---|
[13429] | 87 | |
---|
[17682] | 88 | if ($show_mx) { |
---|
[22971] | 89 | open(HOST, "$hostprog -t mx '$arg' $server 2>&1 |"); |
---|
[17682] | 90 | while (<HOST>) { |
---|
| 91 | if (/mail is handled .*by [ \d]*([^ ]*)$/) { |
---|
| 92 | push(@mx, $1); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | close(HOST); |
---|
[13429] | 96 | } |
---|
[17682] | 97 | chomp (@mx); |
---|
[13429] | 98 | |
---|
[22971] | 99 | open(HOST, "$hostprog -t a '$arg' $server 2>&1 |"); |
---|
[13429] | 100 | while (<HOST>) { |
---|
| 101 | if (/(.*) has address (.*)$/) { |
---|
| 102 | $host = $1; |
---|
| 103 | push(@addr, $2); |
---|
| 104 | } elsif ( /domain name pointer (.*)$/) { |
---|
| 105 | $host = $1; |
---|
| 106 | push(@addr, $arg); |
---|
| 107 | } else { |
---|
| 108 | $error = $error . $_; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | close(HOST); |
---|
| 112 | |
---|
| 113 | if (!$host) { |
---|
| 114 | if ($error =~ /Host not found\./) { |
---|
| 115 | print STDERR "No such host '$arg'.\n"; |
---|
| 116 | } elsif ($error =~ /try again|No recovery/) { |
---|
| 117 | print STDERR "Cannot resolve name '$arg' due to network difficulties.\n"; |
---|
[15351] | 118 | } elsif (!@mx) { |
---|
[13429] | 119 | print STDERR "No such host '$arg'.\n"; |
---|
[17682] | 120 | } elsif (!$show_mx) { |
---|
[13429] | 121 | print STDERR "No address for '$arg'.\n"; |
---|
| 122 | } |
---|
[17682] | 123 | next unless ($show_mx && @mx); |
---|
[13429] | 124 | } |
---|
| 125 | |
---|
| 126 | if ($show_hinfo) { |
---|
[22971] | 127 | open(HOST, "$hostprog -t hinfo '$host' $server 2>&1 |"); |
---|
[13429] | 128 | while (<HOST>) { |
---|
| 129 | if (/host information (.*)$/) { |
---|
| 130 | $hinfo = $1; |
---|
| 131 | $hinfo =~ s: :/:; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if ($long_output) { |
---|
| 137 | print "Desired host:\t$arg\n"; |
---|
[17682] | 138 | if ($host) { |
---|
| 139 | print "Official name:\t$host\n"; |
---|
| 140 | } |
---|
[13429] | 141 | foreach (@addr) { print "Host address:\t$_\n"; } |
---|
| 142 | print "Host info:\t$hinfo\n" if $show_hinfo && $hinfo; |
---|
[15351] | 143 | foreach (@mx) { print "MX address:\t$_\n" if $show_mx; } |
---|
[13429] | 144 | } elsif ($show_host && $host) { |
---|
| 145 | print "$host\n"; |
---|
| 146 | } elsif ($show_addr && @addr) { |
---|
| 147 | foreach (@addr) { print "$_\n"; } |
---|
| 148 | } elsif ($show_hinfo && $hinfo) { |
---|
| 149 | print "$hinfo\n"; |
---|
[15351] | 150 | } elsif ($show_mx && @mx) { |
---|
| 151 | foreach (@mx) { print "$_\n"; } |
---|
[13429] | 152 | } |
---|
| 153 | |
---|
| 154 | print "\n" if $long_output && @ARGV; |
---|
| 155 | } |
---|