#! /usr/bin/env perl

use strict;
use warnings;

use Getopt::Long;

# this is a filter. A list of domains is passed on STDIN
# for each domain, it will check that the DNS record matches
# $myIP and, if it does not, will display the domain on 
# STDOUT.

# Strongly recommend you verify before deleting any web sites, however.

# A sample call is
# ls /var/www/ | grep '^.*\..*$' | grep -v '^quota' | ./doWeHost.pl
# this gets all domains listed in /var/www which contain at least one
# period. Since the quota controls for ISPConfig are in here and have
# a period in their filenames, we use grep -v to exclude them
# that list is then piped to the script on STDIN

# getMyIP
# this finds the first IP address, assuming ifconfig is installed
# and a valid IP is of the form
# inet 192.168.1.32  netmask 255.255.255.0  broadcast 10.111.111.255
sub getMyIP {
  my @ip = grep{ /^\s*inet [\d.]+.*broadcast/ } `ifconfig -a`;
  if ( $ip[0] =~ m/^\s*inet\s+([\d.]+)/ ) { 
     return $1;
   }
}

# the IP address to check against 
my $ip = &getMyIP();
# the record type to check. Blank checks A and CNAME
my $recordType = '';  # defaults to A record
# the dns server to use for lookups
my $dnsServer = '9.9.9.9';

# Check for CLI options
# -r or --recordtype determines type of record to find
# -i or --ip is our local IP
# -d or --dns is the remote DNS server to use
GetOptions( 
      "recordtype:s" => \$recordType,
      "ip:s" => \$ip,
      'dns:s' => \$dnsServer
      );

# go through all domains on STDIN      
while ( my $domain = <> ) {
   #Remove any trailing spaces
   chomp $domain;
   # run dig against it
   my $result = `dig +short \@$dnsServer $domain $recordType`;
   # check if it matches our ip and, if not print to STDOUT
   print "$domain\n" unless $result =~ m/$ip/;
}
