1 | #!/usr/athena/bin/perl |
---|
2 | |
---|
3 | $hostprog = "/usr/athena/bin/host"; |
---|
4 | |
---|
5 | $long_output = $show_host = $show_addr = $show_hinfo = $show_mx = 1; |
---|
6 | $server = "localhost"; |
---|
7 | |
---|
8 | while (@ARGV) { |
---|
9 | my $arg = shift(@ARGV); |
---|
10 | if ($arg eq "-h") { |
---|
11 | $show_host = 1; |
---|
12 | $long_output = $show_addr = $show_hinfo = $show_mx = 0; |
---|
13 | next; |
---|
14 | } elsif ($arg eq "-a") { |
---|
15 | $show_addr = 1; |
---|
16 | $long_output = $show_host = $show_hinfo = $show_mx = 0; |
---|
17 | next; |
---|
18 | } elsif ($arg eq "-i") { |
---|
19 | $show_hinfo = 1; |
---|
20 | $long_output = $show_host = $show_addr = $show_mx = 0; |
---|
21 | next; |
---|
22 | } elsif ($arg eq "-m") { |
---|
23 | $show_mx = 1; |
---|
24 | $long_output = $show_host = $show_addr = $show_hinfo = 0; |
---|
25 | next; |
---|
26 | } elsif ($arg eq "-q") { |
---|
27 | $long_output = $show_host = $show_addr = 1; |
---|
28 | $show_hinfo = $show_mx = 0; |
---|
29 | next; |
---|
30 | } elsif ($arg eq "-ns") { |
---|
31 | if ($arg =~ /[^a-zA-Z0-9.-]/) { |
---|
32 | print STDERR "Bad hostname '$arg'.\n"; |
---|
33 | next; |
---|
34 | } |
---|
35 | $server = shift(@ARGV); |
---|
36 | system "$hostprog -t a $server > /dev/null 2>&1"; |
---|
37 | if ($?) { |
---|
38 | print STDERR "No such host '$server'.\n\n"; |
---|
39 | $server = "localhost"; |
---|
40 | } |
---|
41 | next; |
---|
42 | } elsif ($arg =~ /^-/) { |
---|
43 | print STDERR "Usage: hostinfo <options> <host-names-or-addresses>\n"; |
---|
44 | print STDERR " -h: output only hostname\n"; |
---|
45 | print STDERR " -a: output only address\n"; |
---|
46 | print STDERR " -i: output only host info record\n"; |
---|
47 | print STDERR " -m: output only mx record\n"; |
---|
48 | print STDERR " -q: disable additional query for hinfo & mx\n"; |
---|
49 | print STDERR " -ns <server>: ask specified name server\n"; |
---|
50 | exit 1; |
---|
51 | } |
---|
52 | |
---|
53 | if ($arg =~ /[^a-zA-Z0-9.-]/) { |
---|
54 | print STDERR "Bad hostname '$arg'.\n"; |
---|
55 | next; |
---|
56 | } |
---|
57 | |
---|
58 | $host = $mx = $hinfo = ""; |
---|
59 | @addr = (); |
---|
60 | |
---|
61 | if (!$show_mx) { |
---|
62 | $flags = "-t a"; |
---|
63 | } else { |
---|
64 | $flags = ""; |
---|
65 | } |
---|
66 | |
---|
67 | open(HOST, "$hostprog $flags '$arg' '$server' 2>&1 |"); |
---|
68 | while (<HOST>) { |
---|
69 | if (/(.*) has address (.*)$/) { |
---|
70 | $host = $1; |
---|
71 | push(@addr, $2); |
---|
72 | } elsif ( /domain name pointer (.*)$/) { |
---|
73 | $host = $1; |
---|
74 | push(@addr, $arg); |
---|
75 | } elsif (/mail is handled \(pri=\d+\) by (.*)$/) { |
---|
76 | $mx = $1; |
---|
77 | } else { |
---|
78 | $error = $error . $_; |
---|
79 | } |
---|
80 | } |
---|
81 | close(HOST); |
---|
82 | |
---|
83 | if (!$host) { |
---|
84 | if ($error =~ /Host not found\./) { |
---|
85 | print STDERR "No such host '$arg'.\n"; |
---|
86 | } elsif ($error =~ /try again|No recovery/) { |
---|
87 | print STDERR "Cannot resolve name '$arg' due to network difficulties.\n"; |
---|
88 | } elsif (!$mx) { |
---|
89 | print STDERR "No such host '$arg'.\n"; |
---|
90 | } else { |
---|
91 | print STDERR "No address for '$arg'.\n"; |
---|
92 | } |
---|
93 | next; |
---|
94 | } |
---|
95 | |
---|
96 | if ($show_hinfo) { |
---|
97 | open(HOST, "$hostprog -t hinfo '$host' '$server' 2>&1 |"); |
---|
98 | while (<HOST>) { |
---|
99 | if (/host information (.*)$/) { |
---|
100 | $hinfo = $1; |
---|
101 | $hinfo =~ s: :/:; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if ($long_output) { |
---|
107 | print "Desired host:\t$arg\n"; |
---|
108 | print "Official name:\t$host\n"; |
---|
109 | foreach (@addr) { print "Host address:\t$_\n"; } |
---|
110 | print "Host info:\t$hinfo\n" if $show_hinfo && $hinfo; |
---|
111 | print "MX address:\t$mx\n" if $show_mx && $mx; |
---|
112 | } elsif ($show_host && $host) { |
---|
113 | print "$host\n"; |
---|
114 | } elsif ($show_addr && @addr) { |
---|
115 | foreach (@addr) { print "$_\n"; } |
---|
116 | } elsif ($show_hinfo && $hinfo) { |
---|
117 | print "$hinfo\n"; |
---|
118 | } elsif ($show_mx && $mx) { |
---|
119 | print "$mx\n"; |
---|
120 | } |
---|
121 | |
---|
122 | print "\n" if $long_output && @ARGV; |
---|
123 | } |
---|