I am new to perl and attempting to write a script that will do a reverse dns
lookup on an ip, store this result into a file, then read the file in order
to do a whois lookup. The whois lookup answer should also be written to a
seperate file. I have the reverse dns lookup working, but when I also call
my whois subroutine, nothing is written to file for the the reverse dns (I
get a file size of 0). Can someone tell me where I am going wrong here?


#!/usr/bin/perl
#
# repeat outbound connection check
# usage: ./ipcheck.pl <ip>
# ARGV[0] = ip address

# required for reverse DNS lookup, whois
# to install, as root run: perl -MCPAN -e 'install Net::<module_name>'
use strict;
use Net::DNS;
use Net::IP;
use Net::ParseWhois;

#check for user input and print usage instructions if nothing is supplied
if (@ARGV <= 0)
    {
        die ("Usage: ./ipcheck.pl <ip> <options>:\n");
    }

# get ip address from command line input
my $ip = $ARGV[0];

print ("Creating results file $ip-results.txt\n");
#call file creation subroutine
#&createfile;

print ("Attempting reverse DNS lookup\n");
# call reverse DNS lookup subroutine
&reversedns;

print ("Attempting whois lookup\n");
#call whois subroutine
&whois;

print ("Program executed successfully");
#
-----------------------------subroutines------------------------------------------

# create file subroutine
sub createfile {

# opens the <ip>-results.txt file to write the resuls to. Prints error
message if file cannot be opened
# prints results to the dir thescript is ran from
open (RESULTS, ">$ip-results.txt") || die ("Could not open results file");
}

# reverse dns subroutine
sub reversedns {

    #open file to write to <ip>-results-reverse-dns.txt
    open (RESULTSDNS, ">$ip-results-reverse-dns.txt") || die ("Could not
open results file");

    #gets the ip address from the command line
    my $ip = $ARGV[0];

    # Creates a resolver for resolving the reverse DNS lookup.
    my $res = Net::DNS::Resolver->new;

    # Creates an IP object.
    my $target= new Net::IP($ip) or die("Unable to ip object for $ip\n");

     # create the reverse lookup DNS name
    # octets in the IP address need to be reversed).
      my $target_IP = join('.', reverse split(/\./,
$target->ip())).".in-addr.arpa";

      # Perform a query on the produced name. (note we want the PTR records
for the name).
      my $q = $res->query("$target_IP", "PTR");

      if($q)
          {
                # If the query on the produced name is valid then get the
answer.
                my $r = ($q->answer)[0];

                # If the query returns a result other than the PTR record,
print error and die.
                if($r->type ne "PTR")
                {
                    die ("Error: query returned result other than PTR
record");
                 }

                # prints the answer to file (<ip>-results-reverse-dns.txt)
                print RESULTSDNS ($r->rdatastr."\n");
            # close file when done writing
            #close (RESULTSDNS);
          }
}

# whois subroutine
sub whois {

    # open file to read in reverse dns answer
    open (RESULTSRDNS, "$ip-results-reverse-dns.txt") || die ("Could not
open reverse dns results file to read in reverse dns answer");

    # read in reverse dns answer to use as domain variable from
<ip>-results.txt
        my @domain = <RESULTSRDNS>;

    # closes the file when done reading
    close (RESULTSRDNS);

    while (my @domain == "")
    {

    #open the file to write to
    open (RESULTSWHOIS, ">$ip-results-whois.txt") || die ("Could not open
temp results file for writing whois information");

        my $soa_domain;

        FIND_SOA: {
            my $res = new Net::DNS::Resolver;

            my $q = $res->send(@domain, "SOA");

            for my $sec (qw(answer authority))
                {
                        my $meth = $q->can($sec) or next;

                        for my $rec ($meth->($q))
                            {
                            next unless $rec->isa('Net::DNS::RR::SOA');

                            $soa_domain = $rec->name;

                            last FIND_SOA if $soa_domain;
                            }
                }
        }

        #die "Couldn't find SOA for $domain\n" unless defined $soa_domain;
        $soa_domain ||= @domain;

        my $whois = new Net::ParseWhois::Domain($soa_domain);
        warn "Couldn't connect to Whois server$/", next unless $whois;
        warn "No Whois match for $soa_domain$/", next unless $whois->ok;

        # print out whois match
    print RESULTSWHOIS ("Whois Information:\n");
    print RESULTSWHOIS "Whois Server: ", $whois->whois_server, $/;
        print RESULTSWHOIS $/;

        print RESULTSWHOIS "Registrar: ", $whois->registrar, $/;
        print RESULTSWHOIS "Domain: ", $whois->domain, $/;
        print RESULTSWHOIS "Name: ", $whois->name, $/;
        print RESULTSWHOIS "Tag: ", $whois->tag, $/;
        print RESULTSWHOIS $/;

        print RESULTSWHOIS "Address:", $/;
        print RESULTSWHOIS "\t", $_, $/ for $whois->address;
        print RESULTSWHOIS $/;

        print RESULTSWHOIS "Country: ", $whois->country, $/;
        print RESULTSWHOIS $/;

        print RESULTSWHOIS "Name Servers:", $/;
        printf RESULTSWHOIS "\t%s (%s)$/", @$_ for @{$whois->servers};
        print RESULTSWHOIS $/;

        if (my $c = $whois->contacts)
        {
            print RESULTSWHOIS "Contacts:", $/;
            for my $t (sort keys %$c)
                {
                        print RESULTSWHOIS " " x 4, $t, ":", $/;
                        print RESULTSWHOIS "\t", $_, $/ for @{$c->{$t}};
                }
            print RESULTSWHOIS $/;
        }

        print RESULTSWHOIS "Record created: ", $whois->record_created, $/;
        print RESULTSWHOIS "Record updated: ", $whois->record_updated, $/;
        print RESULTSWHOIS "Record expires: ", $whois->record_expires, $/;

        print RESULTSWHOIS "=" x 76, $/ if @ARGV;

    # close the file when done writing
    close (RESULTSWHOIS);
  }
}

Reply via email to