Script to do DNS lookups? 
Author Message
 Script to do DNS lookups?

Has anyone ever written a perl script to do DNS lookups?  Yes, I know
about gethostbyname() and gethostbyaddr(); they don't give all the
information I need, eg. some hosts have several IPs which can't be
picked up with gethostbyname().  So, anyone have some DNS lookup code?

Thanks in advance,
-- pi



Mon, 17 Jun 1996 00:25:07 GMT  
 Script to do DNS lookups?

Quote:

>information I need, eg. some hosts have several IPs which can't be
>picked up with gethostbyname().

If that's the case, your gethostbyname is broken.  Some specimens (inc.
SUNos) have trouble at more than 10 addresses or so, but up to that you
should get all of them. Don't forget to make the last element an array
so that you can pick them all up.

Asking the DNS isn't the same thing as doing gethostbyname - depends how
the system is set up. It may have an /etc/hosts or an NIS server which
is asked first.

Ian



Mon, 17 Jun 1996 00:46:35 GMT  
 Script to do DNS lookups?


: >information I need, eg. some hosts have several IPs which can't be
: >picked up with gethostbyname().

: If that's the case, your gethostbyname is broken.  Some specimens (inc.
: SUNos) have trouble at more than 10 addresses or so, but up to that you
: should get all of them. Don't forget to make the last element an array
: so that you can pick them all up.

True Ian, but there is still information in DNS that is not accessible
via gethostname (getting MINFO or MX records come to mind as some possibly
useful information).  

The question still stands.  Does anyone have a clean way of getting DNS
information?  I find myself calling nslookup, dumping the info to a file
and then parsing the file.  Ugly, but it does work.

n
|==========================================================================|
|Frank Stutzman                          | The network is the computer     |
|SynOptics, Communcations, Inc           | The T-connector is the network  |
|Santa Clara, Ca                         | The computer is a T-connector?  |

|==========================================================================|



Mon, 17 Jun 1996 01:59:38 GMT  
 Script to do DNS lookups?

Quote:
>>>>> Frank Stutzman writes:
> The question still stands.  Does anyone have a clean way of getting DNS
> information?  I find myself calling nslookup, dumping the info to a file
> and then parsing the file.  Ugly, but it does work.

There's a DNS resolver library written in perl by Marc Horowitz that
implements most of what you'd want.  Archie showed that it was ftp'able
from bongo.cc.utexas.edu in /source/languages/perl-stuff/resolv; it's likely
other places as well.

        -Lucien
--
-----------------------------------------------------------------------
Lucien Van Elsen                                          IBM  Research



Mon, 17 Jun 1996 03:13:39 GMT  
 Script to do DNS lookups?

Quote:
> >>>>> Frank Stutzman writes:

> > The question still stands.  Does anyone have a clean way of getting DNS
> > information?  I find myself calling nslookup, dumping the info to a file
> > and then parsing the file.  Ugly, but it does work.

> There's a DNS resolver library written in perl by Marc Horowitz that
> implements most of what you'd want.  Archie showed that it was ftp'able
> from bongo.cc.utexas.edu in /source/languages/perl-stuff/resolv; it's likely
> other places as well.

Here is something that may be useful for bulk examination of DNS data.
It has an interface quite similar to the one employed by find.pl, which
comes with Perl.

I have included an example of how to use it -- a program that looks at
MX records to discover which addresses should be considered equivalent
to our central mail server by sendmail.

Note, however, that I can not guarantee that this is bulletproof.  In
particular, I had problems when dig returned SERVFAIL.  I haven't yet
got around to making dnsfind fail cleanly in this instance because it
isn't convenient having named configured to cause dig to return
SERVFAIL...

#!/bin/sh
# This is a shell archive (shar 3.32)

#
# existing files WILL be overwritten
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   6163 -rw-r----- dnsfind.pl
#   2259 -rwxr-x--- find-mx-sol
#
if touch 2>&1 | fgrep 'amc' > /dev/null
 then TOUCH=touch
 else TOUCH=true
fi
# ============= dnsfind.pl ==============
echo "x - extracting dnsfind.pl (Text)"
sed 's/^X//' << 'SHAR_EOF' > dnsfind.pl &&
X# dnsfind - like find(1), but for DNS data
X#
X# $Id: dnsfind.pl,v 1.0 1993/11/25 23:12:33 tim Exp $
X# $Source: /tmp_mnt/src/util/perl/RCS/dnsfind.pl,v $
X#
X# Copyright (c) Tim Cook, 1993, 1994.
X#
X# SYNOPSIS
X#      #!/usr/local/bin/perl
X#      require 'dnsfind.pl';
X#      &dnsfind ('my.domain.com', '1.128.in-addr.arpa');
X#      sub dnswanted {
X#          printf ("%40s %10s %s\n", $zone, $type, $value);
X#      }
X#
X# DESCRIPTION
X#      Recursively seaches DNS zones, and calls the user-defined
X#      subroutine "dnswanted" for each record found.  Fields from the
X#      record are put in the following global variables:
X#
X#          $parent_zone        The parent zone which is being
X#                              examined via a "dig axfr" (zone
X#                              transfer) command.
X#          $server             The canonical name of the primary
X#                              server for this (parent) zone.  From
X#                              the SOA record.
X#          $zone               Fully-qualified domain-name.
X#          $timeout            Seconds before record is out of date.
X#          $type               Record type (A, MX, CNAME, NS, etc).
X#          $value              Value of the record.
X
X#
X# Safe return from "require"
X#
X1;
X
X##############################################################################
X# dnsfind
X#
Xsub dnsfind {

X#   GLOBAL ($parent_zone)
X#   GLOBAL ($server)
X#   GLOBAL ($zone)
X#   GLOBAL ($timeout)
X#   GLOBAL ($type)
X#   GLOBAL ($value)
X#   GLOBAL (%_zones_searched)
X
X    local ($parent_zone_l);
X    local ($server_l);
X    local ($zone_l);
X    local ($timeout_l);
X    local ($type_l);
X    local ($value_l);


X    local ($record);
X    local ($tmp);
X

X       #
X       # Find primary
X       #


X       if ($#data != 0) {
X           die "Can't find primary server for $parent_zone_l\n";
X       }

X       $parent_zone_l = $data[0];
X       $server_l = $data[3];
X       if (! length ($server_l)) {
X           die "Can't find primary server for $parent_zone_l\n";
X       }
X
X       #
X       # Avoid this zone if we have already searched it
X       #
X       $tmp = $parent_zone_l;
X       $tmp =~ tr/[A-Z]/[a-z]/;
X       next if ($_zones_searched{$tmp});
X
X       #
X       # Transfer zone
X       #
X       ###print ("[AXFR] $parent_zone_l\n");


X           die "Can't transfer $parent_zone_l from $server_l\n";
X       }
X
X       #
X       # Note this zone as having been searched
X       #
X       $tmp = $parent_zone_l;
X       $tmp =~ tr/[A-Z]/[a-z]/;
X       $_zones_searched{$tmp} = 1;
X
X       #
X       # Process each record
X       #

X           ($zone_l, $timeout_l, $type_l, $value_l) = split ('\s+', $record,
X                                                     4);
X           #
X           # Call the user-supplied subroutine
X           #
X           $parent_zone = $parent_zone_l;
X           $server = $server_l;
X           $zone = $zone_l;
X           $timeout = $timeout_l;
X           $type = $type_l;
X           $value = $value_l;
X           &dnswanted ();
X
X           #
X           # Is it a sub-zone, and have we not searched it?
X           #
X           next if (&strcasecmp ($type_l, 'NS') != 0);
X           next if (! &is_sub_zone ($zone_l, $parent_zone_l));

X           #
X           # Save it to search later
X           #

X       }
X
X       #
X       # Recurse (depth-first) for sub-zones
X       #

X    }
X}
X
X
X##############################################################################
X# is_sub_zone
X#
Xsub is_sub_zone {


X
X    #
X    # To be a sub-zone, you should have a dot, then the parent zone as
X    # a suffix.
X    #
X
X    $parent_zone = '.' . $parent_zone;
X
X    if (length ($zone) <= length ($parent_zone)) {
X       return 0;
X    }
X
X    $parent_zone =~ tr/[A-Z]/[a-z]/;
X    $zone =~ tr/[A-Z]/[a-z]/;
X
X    if (substr ($zone, 0 - length ($parent_zone)) ne $parent_zone) {
X       return 0;
X    }
X
X    return 1;
X}
X
X
X##############################################################################
X# dig -        Simple DNS lookup, using dig(1).
X#
Xsub dig {



X
X    local ($type_lcase);

X    local ($_);
X
X    $type_lcase = $type;
X    $type_lcase =~ tr/[A-Z]/[a-z]/;
X
X    if (length ($server)) {

X    } else {

X    }
X
X    #
X    # The return status will be 768 (3 * 256) for NXDOMAIN
X    #

X       die "$prog: $program: exit status $?\n";
X    }
X
X    #
X    # Remove comments and newlines
X    #


X       s/\s*\n$//;
X       s/\s*;.*$//;
X       if (! /^\s*$/) {

X       }
X    }
X
X    #
X    # Collapse continuations
X    #

X    while (1) {



X           s/\s*\($//;

X           if ($cont =~ /\)$/) {
X               $cont =~ s/\s*\)$//;
X               $_ = $_ . ' ' . $cont;
X           } else {
X               $_ = $_ . ' ' . $cont . ' (';
X           }
X       }

X    }
X

X}
X
X
X########################################################################
X# exec_and_capture -    Run a command and capture its output in an array
X#
Xsub exec_and_capture {


X
X    local ($tmp);
X    local ($pid);
X
X    pipe (_PIPE_OUT, _PIPE_IN) || die "$prog: pipe: $!\n";
X
X    #
X    # Flush stdout and stderr before forking
X    #
X    $tmp = $|;
X    select (STDERR); $| = 1; print (STDERR "");
X    select (STDOUT); $| = 1; print (STDOUT "");
X    $| = $tmp;
X
X    $pid = fork ();
X
X    # Error ?
X    if ($pid eq "") {
X       die "$prog: fork: $!\n"; }
X
X    # Child ?
X    if ($pid == 0) {
X
X       close (_PIPE_OUT);
X       open (STDOUT, '>&_PIPE_IN') || die "$prog: stdout: $!\n";
X

X    }
X
X    # Parent
X    close (_PIPE_IN);
X

X    waitpid ($pid, 0);
X

X}
X
X
X##############################################################################
X# strcasecmp - Compare strings while ignoring case
X#
Xsub strcasecmp {


X
X    $s1 =~ tr/[A-Z]/[a-z]/;
X    $s2 =~ tr/[A-Z]/[a-z]/;
X
X    return ($s1 cmp $s2);
X}
SHAR_EOF
$TOUCH -am 0102213094 dnsfind.pl &&
chmod 0640 dnsfind.pl ||
echo "restore of dnsfind.pl failed"
set `wc -c dnsfind.pl`;Wc_c=$1
if test "$Wc_c" != "6163"; then
        echo original size 6163, current size $Wc_c
fi
# ============= find-mx-sol ==============
echo "x - extracting find-mx-sol (Text)"
sed 's/^X//' << 'SHAR_EOF' > find-mx-sol &&
X#!/usr/local/bin/perl
X# find-mx-sol -        Find all names which list sol as "best-MX"
X#
X# $Id: find-mx-sol,v 1.0 1993/11/25 23:48:44 tim Exp $
X# $Source: /tmp_mnt/src/config/etc/sendmail.cw/find-mx-sol,v $
X#
X# Copyright (c) Tim Cook, 1993, 1994.
X#
X
Xrequire 'dnsfind.pl';
X
X
X#############################################################################
X# Main Program
X#
X
X&dnsfind ('deakin.edu.au', 'deakin.oz.au');
X
Xforeach $zone (keys (%exchangers)) {

X
X    #
X    # If sol is one of the most preferred MX's for this zone, then
X    # save it
X    #


X    }
X}
X
X#
X# Custom sort our output

X
X#
X# Print it
X#

X    print ($zone, "\n");
X}
X
Xexit (0);
X
X
X#############################################################################
X# dnswanted -  Called for each record found by dnsfind
X#
Xsub dnswanted {
X    $type =~ tr/[a-z]/[A-Z]/;
X
X    if ($type eq 'MX') {
X       local ($pref);
X       local ($exchanger);
X
X       ($pref, $exchanger) = split ('\s+', $value);
X       $zone = &cleanhost ($zone);
X       $exchanger = &cleanhost ($exchanger);
X
X       if (defined ($lowest_pref{$zone})) {
X           if ($pref == $lowest_pref{$zone}) {
X               $exchangers{$zone} = $exchangers{$zone} . '|' .
X                   $exchanger;
X           } else {
X               if ($pref < $lowest_pref{$zone}) {
X                   $lowest_pref{$zone} = $pref;
X                  
...

read more »



Thu, 20 Jun 1996 18:36:21 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. that great DNS lookup script

2. Reverse dns lookup within mailform script

3. DNS lookup

4. DNS lookups through perl

5. Reverse DNS lookup

6. DNS lookup

7. DNS Lookups

8. fork for DNS lookup - help

9. Reverse DNS Lookup

10. Fast DNS lookups?

11. dns lookup on choosen name server?

12. DNS lookup with gethostbyaddr?

 

 
Powered by phpBB® Forum Software