Results 1 to 6 of 6

Thread: Perl: Help with Net::Nslookup script

  1. #1
    Join Date
    Jun 2009
    Location
    Raleigh, NC
    Beans
    584
    Distro
    Ubuntu 14.04 Trusty Tahr

    Perl: Help with Net::Nslookup script

    It returns the same IP over and over. This is my first perl script ever

    use Net::Nslookup;
    open (FILE, 'list.txt');
    open (MYFILE, '>>dnsResults.csv');
    foreach $record (<FILE>) {
    my $r = null;
    my $r = nslookup "$record";
    print MYFILE "$record,$r \n";
    }
    close (MYFILE, 'dnsResults.csv');
    Ubuntu 14.04 LTS / E6750 @ 3.52GHz / Zalman 9500 / GIGABYTE GA-P35-DS3L / Mushkin Silver/Blackline 4GB DDR2800 / MSI 8600GT GPU / OCZ 120GB SSD / Antec P180B
    Ubuntu User 31267

  2. #2
    Join Date
    Jun 2009
    Location
    Raleigh, NC
    Beans
    584
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Perl n00b...Help with Net::Nslookup script

    inside test.txt I have

    www.google.com
    www.yahoo.com
    127.0.0.1

    Only thing returned is 127.0.0.1

    nameserver in resolv.conf is 8.8.8.8...bash works fine :(

    use Net::Ping;
    open (FILE, 'test.txt');
    foreach $record (<FILE>) {
    #print "ping $record \t";
    $p=Net::Ping->new();
    my $ping = $p->ping($record, 2);
    if($ping) {print $record; };
    }
    Ubuntu 14.04 LTS / E6750 @ 3.52GHz / Zalman 9500 / GIGABYTE GA-P35-DS3L / Mushkin Silver/Blackline 4GB DDR2800 / MSI 8600GT GPU / OCZ 120GB SSD / Antec P180B
    Ubuntu User 31267

  3. #3
    Join Date
    Feb 2008
    Location
    Pelican Bay Correctional
    Beans
    Hidden!

    Re: Perl: Help with Net::Nslookup script

    a post at here says Just a warning: per Paul Vixie, nslookup is broken. Use dig
    having spent ALL day banging on dns and zone edits, I banged this script out.

    Code:
    vi checkdns.pl
    and add
    Code:
    #!/usr/bin/perl
    $file=$ARGV[0];   
    chomp($file);
    @list = `cat $file`;
    
    $filedns=$ARGV[1]; 
    chomp($filedns);
    @listdns = `cat $filedns`;
    
    foreach $dns(@listdns) {
    chomp($dns);
    print "\n--------------\n$dns\n";
    
    for ($count = 2; $count >= 1; $count--) {
            foreach $domain(@list) {
                    chomp($domain);
                    $ns = `dig $domain +short`;
                    chomp($ns);
                    $ns =~s/\n/\t/g;
                    print "$domain\t$ns\n";
            }
    
    }
    
    }
    Save it and exit the editor. make it executable using
    Code:
    chmod 700 checkdns.pl
    Put a list of the domains in domains.txt.
    Save and exit domains.txt
    Put a list of dns servers in dnsservers.txt to check against (I used IPs in case RDNS is NOT set...)
    save and exit dnsservers.txt

    Now the Magic:
    Code:
    checkdns.pl domains.txt dnsservers.txt
    I didn't author it, I found it and modified it (I removed "$dns" in $ns = `dig $dns $domain +short`;) because I only wanted the host and the A Record from all the nameservers.

    I really hope this alternative can benefit you as it did me.
    What's a landing but a take off in reverse?

  4. #4

    Re: Perl: Help with Net::Nslookup script

    Habitual, the post you linked to is from 2000.

    I tried to install Net::Nslookup from CPAN to see if I could replicate the problem, but the automated tests seem to depend on www.boston.com being found somewhere it isn't (at least not for me). I don't have the patience to fiddle with CPAN tonight, but I'll toss some advice your (OP's) way anyway:

    1_ Always use strict and use warnings. Your first attempt has several problems that one or both of these would complain about. I don't think any of those problems are what you're actually wondering about here, but it can't hurt.

    2_ Put code in [code] tags when you post it on this forum -- just makes it easier to read.

    3_ It's normally better to use while instead of for(each) to iterate over a file in Perl. Not a big deal but you might want to get into the habit. Explanation

    4_ Have you done `perldoc Net::Nslookup` and are you sure you're using it the right way? Most of the online examples I found seemed to call it with keyword arguments (e.g. domain => "google.com", not just "google.com").

    Also note that Google doesn't always respond to plain pinging -- not sure why that is but it's true. If you use Net:: Ping you may want to use other domains for testing purposes. Universities are usually good.

  5. #5
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Perl: Help with Net::Nslookup script

    I tried playing with Net::Nslookup and couldn't get it to install either. Seems there's an issue with this module. However, there's another one that might do what you're trying to do, Net:NS::Nslookup. I just scribbled together this quickie that might do what you want:

    Code:
    #!/usr/bin/perl
    
    
    use warnings;
    use strict;
    use Net::DNS::Nslookup;
    
    
    my $outfile = "dnsResults.csv";
    open( OUTPUT, ">", $outfile ) || die "Can't open file '$outfile' for writing: $!";                                                   
                                                                                                                                         
    my $infile = $ARGV[0] || die "Need to enter a file with a list of IP Addresses";                                                     
    open( INFILE, "<", $infile ) || die "Can't open the input file '$infile' for reading: $!";                                           
    while (<INFILE>) {                                                                                                                   
            chomp;                                                                                                                       
            my $dnsLookup = Net::DNS::Nslookup->get_ips( $_ );                                                                           
            print OUTPUT "$dnsLookup\n";                                                                                                 
    }                                                                                                                                    
    close( OUTPUT );                                                                                                                     
    close( INFILE );
    If you run it with:

    Code:
    perl nslookup.pl <file_with_sitenames>
    the output will be (written to a file called dnsResults.csv):

    Code:
    www.google.com,173.194.73.147
    www.google.com,173.194.73.105
    www.google.com,173.194.73.106
    www.google.com,173.194.73.99
    www.google.com,173.194.73.103
    www.google.com,173.194.73.104
    www.ubuntuforums.org,91.189.94.12
    www.cnn.com,157.166.240.13
    www.cnet.com,64.30.224.103
    www.perl.org,207.171.7.51
    www.perl.org,207.171.7.41
    I think that at least should give you a starting point.

  6. #6

    Re: Perl: Help with Net::Nslookup script

    I submitted a bug report on CPAN and Net::Nslookup has been fixed now.

    The only other thing that I noticed after trying to use the module myself is that you have to chomp the input -- "www.google.com\n" won't resolve.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •