View Single Post
Old 27th November 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

As an alternative for the "dig-then" sh script, I wrote a Perl script that does the same thing.
Code:
$cat select-domains

#!/usr/bin/perl -w

# $Id: select-domains,v 1.3 2008/11/27 01:35:49 j65nko Exp $

#  Copyright (c) 2008 J65nko <Administrator daemonforums.org>
# 
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
# 
#  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 

use strict ;

my $filename = "domain-addr.txt" ; 
my @domains ;
my $IP ;

# scan VirtualHost input file for ServerName entries, extract the domain name
# and store in array '@domains'

print "\n$0: Scanning domain names ....\n" ;

while (<>) {
    if ( /^\s*<VirtualHost/ .. /^\s*<\/VirtualHost>/ ) {
        if ( /^\s*ServerName\s+(\S+)$/ ) {
            print "\nDomain: $1" ;
            push @domains,$1
        }
    }
}

open ( FILE, "> $filename" ) or die "Cannot open $filename !" ;

print  "\n\n$0: Starting to resolve domains....\n" ;

# for every domain in array, use 'dig' to lookup IP address

for ( @domains ) {
    print "\nResolving: $_ " ;
    $IP = '' ;
    $IP = `dig +short -t A $_` ; # Use 'dig' to resolve domain
    print FILE "$_=" ;
    if ( $IP =~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*/ ) {
        $IP =  $1 ;
        chomp $IP ;
        print "  IP: $IP" ;
        print FILE "$IP\n" ;
    } else {
         print " IP: Not resolved" ; 
         print FILE "\n" ;
    }
} 

print "\n\n$0: Finished!\n" ;

END {
close (FILE) or print "Couldn't close file: $filename"  ;
}
# --- end of program
The advantage is that it logs information to stdout, what it is actually doing.
A sample run, where again 'virt_orig.txt" is the VirtualHost configuration file
Code:
$ select-domains virt_orig.txt | tee logfile 

./select-domains: Scanning domain names ....

Domain: google.com
Domain: ixcdgorilla.org
Domain: monkey.org
Domain: daemonforums.org

./select-domains: Starting to resolve domains....

Resolving: google.com   IP: 209.85.171.100
Resolving: ixcdgorilla.org  IP: Not resolved
Resolving: monkey.org   IP: 152.160.49.201
Resolving: daemonforums.org   IP: 67.205.67.40

./select-domains: Finished!
By using the "tee" program, the logged messages to the screen, are also saved in the file 'logfile".
The actual results are in the file "domain-addr.txt":
Code:
$ cat  domain-addr.txt
  
google.com=209.85.171.100
ixcdgorilla.org=
monkey.org=152.160.49.201
daemonforums.org=67.205.67.40
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote