source: trunk/athena/bin/hostinfo/hostinfo.pl @ 22972

Revision 22972, 4.1 KB checked in by rbasch, 16 years ago (diff)
* athena/bin/hostinfo/hostinfo.pl: Change paths of perl and host executables from /usr/athena/bin to /usr/bin.
  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2# $Id: hostinfo.pl,v 1.5 2002-06-11 21:12:59 jweiss Exp $
3
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
22$hostprog = "/usr/bin/host";
23
24$long_output = $show_host = $show_addr = $show_hinfo = $show_mx = 1;
25$server = "";
26
27sub 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
38if (! @ARGV) {
39    &usage();
40}
41
42while (@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") {
65        $server = shift(@ARGV) || &usage();
66        if ($server =~ /[^a-zA-Z0-9.-]/) {
67            print STDERR "Bad hostname '$server'.\n";
68            &usage();
69        }
70        system "$hostprog -t a $server > /dev/null 2>&1";
71        if ($?) {
72            print STDERR "No such host '$server'.\n\n";
73            $server = "";
74        }
75        next;
76    } elsif ($arg =~ /^-/) {
77        &usage();
78    }
79
80    if ($arg =~ /[^a-zA-Z0-9.-]/) {
81        print STDERR "Bad hostname '$arg'.\n";
82        next;
83    }
84
85    $host = $hinfo = "";
86    @addr = @mx = ();
87
88    if ($show_mx) {
89        open(HOST, "$hostprog -t mx '$arg' $server 2>&1 |");
90        while (<HOST>) {
91            if (/mail is handled .*by [ \d]*([^ ]*)$/) {
92                push(@mx, $1);
93            }
94        }
95        close(HOST);
96    }
97    chomp (@mx);
98
99    open(HOST, "$hostprog -t a '$arg' $server 2>&1 |");
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";
118        } elsif (!@mx) {
119            print STDERR "No such host '$arg'.\n";
120        } elsif (!$show_mx) {
121            print STDERR "No address for '$arg'.\n";
122        }
123        next unless ($show_mx && @mx);
124    }
125
126    if ($show_hinfo) {
127        open(HOST, "$hostprog -t hinfo '$host' $server 2>&1 |");
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";
138        if ($host) {
139            print "Official name:\t$host\n";
140        }
141        foreach (@addr) { print "Host address:\t$_\n"; }
142        print "Host info:\t$hinfo\n" if $show_hinfo && $hinfo;
143        foreach (@mx) { print "MX address:\t$_\n" if $show_mx; }
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";
150    } elsif ($show_mx && @mx) {
151        foreach (@mx) { print "$_\n"; }
152    }
153
154    print "\n" if $long_output && @ARGV;
155}
Note: See TracBrowser for help on using the repository browser.