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

Revision 13765, 4.0 KB checked in by danw, 25 years ago (diff)
add rcsid, license, descriptive comment
  • Property svn:executable set to *
Line 
1#!/usr/athena/bin/perl
2# $Id: hostinfo.pl,v 1.3 1999-10-18 18:43:10 danw 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/athena/bin/host";
23
24$long_output = $show_host = $show_addr = $show_hinfo = $show_mx = 1;
25$server = "localhost";
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        if ($arg =~ /[^a-zA-Z0-9.-]/) {
66            print STDERR "Bad hostname '$arg'.\n";
67            next;
68        }
69        $server = shift(@ARGV);
70        system "$hostprog -t a $server > /dev/null 2>&1";
71        if ($?) {
72            print STDERR "No such host '$server'.\n\n";
73            $server = "localhost";
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 = $mx = $hinfo = "";
86    @addr = ();
87
88    if (!$show_mx) {
89        $flags = "-t a";
90    } else {
91        $flags = "";
92    }
93
94    open(HOST, "$hostprog $flags '$arg' '$server' 2>&1 |");
95    while (<HOST>) {
96        if (/(.*) has address (.*)$/) {
97            $host = $1;
98            push(@addr, $2);
99        } elsif ( /domain name pointer (.*)$/) {
100            $host = $1;
101            push(@addr, $arg);
102        } elsif (/mail is handled \(pri=\d+\) by (.*)$/) {
103            $mx = $1;
104        } else {
105            $error = $error . $_;
106        }
107    }
108    close(HOST);
109
110    if (!$host) {
111        if ($error =~ /Host not found\./) {
112            print STDERR "No such host '$arg'.\n";
113        } elsif ($error =~ /try again|No recovery/) {
114            print STDERR "Cannot resolve name '$arg' due to network difficulties.\n";
115        } elsif (!$mx) {
116            print STDERR "No such host '$arg'.\n";
117        } else {
118            print STDERR "No address for '$arg'.\n";
119        }
120        next;
121    }
122
123    if ($show_hinfo) {
124        open(HOST, "$hostprog -t hinfo '$host' '$server' 2>&1 |");
125        while (<HOST>) {
126            if (/host information (.*)$/) {
127                $hinfo = $1;
128                $hinfo =~ s: :/:;
129            }
130        }
131    }
132
133    if ($long_output) {
134        print "Desired host:\t$arg\n";
135        print "Official name:\t$host\n";
136        foreach (@addr) { print "Host address:\t$_\n"; }
137        print "Host info:\t$hinfo\n" if $show_hinfo && $hinfo;
138        print "MX address:\t$mx\n" if $show_mx && $mx;
139    } elsif ($show_host && $host) {
140        print "$host\n";
141    } elsif ($show_addr && @addr) {
142        foreach (@addr) { print "$_\n"; }
143    } elsif ($show_hinfo && $hinfo) {
144        print "$hinfo\n";
145    } elsif ($show_mx && $mx) {
146        print "$mx\n";
147    }
148
149    print "\n" if $long_output && @ARGV;
150}
Note: See TracBrowser for help on using the repository browser.