Thanks for the help John. I have made the changes you suggested and managed
to get everything working properly. One more question though that is
completely different, how do I allow users to specify switches on the
command line in any order to be used in my program. For example, I want the
syntax to be ./ipcheck.pl <ip> <port> {options specified in any order}. I
know I can get the value for the ip using $ip=$ARGV[0] and port using
$port=$ARGV[1] but how would I get the remaining options?
Example to help clarify: ./ipcheck 10.10.10.1 80 -n -a -r
where -n -a -r are options in no particular order, 10.10.10.1 is the ip and
80 is the port.
On Sun, Nov 30, 2008 at 11:17 PM, John W. Krahn <[EMAIL PROTECTED]> wrote:
> blake askew wrote:
>
>> 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
>>
>
> use warnings;
> use strict;
>
> #
>> # 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)
>>
>
> An array can never have a value less than 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;
>>
>
> The proper form for calling a subroutine in perl is:
>
> 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");
>>
>
> You should include the $! variable in the error message so you know *why*
> it failed. You might also want to include the file name so you know which
> file could not be opened.
>
>
> }
>>
>> # 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");
>>
>
> See advice above for previous open().
>
>
> #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");
>>
>
> perldoc -q quoting
>
>
> 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");
>>
>
> See advice above for previous open().
>
>
> # 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 == "")
>>
>
> You are creating a new empty @domain variable and then comparing it
> numerically to a string so inside this while loop @domain will always be
> empty and the loop will never end.
>
>
> {
>>
>> #open the file to write to
>> open (RESULTSWHOIS, ">$ip-results-whois.txt") || die ("Could not open
>> temp results file for writing whois information");
>>
>
> See advice above for previous open().
>
>
> 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;
>>
>
> If $soa_domain is 0 or empty you are assigning it the number of elements of
> @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;
>>
>
> Why are you using $/ instead of "\n"?
>
>
> # close the file when done writing
>> close (RESULTSWHOIS);
>> }
>> }
>>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>