#!/usr/bin/perl ########################################################## # Forward/Reverse DNS verification # # Scott Baker - 2014-06-06 ########################################################## use strict; #use Data::Dump::Color; use Net::DNS; my $res = Net::DNS::Resolver->new(); my $ip_count = scalar(@ARGV); if ($ip_count == 0) { die(usage()); } my $count = 1; my $text_status = ""; my $status = { 'good' => color('46_on_22') . '* Good *' . color(), 'bad' => color('208_on_88') . '* Error *' . color(), }; foreach my $ip (@ARGV) { my $rdns = rdns($ip); my $forward = ''; print "Checking: $ip\n"; if ($rdns) { $forward = resolve($rdns); if ($ip eq $forward) { print color(34); $text_status = $status->{good} } else { print color(160); $text_status = $status->{bad} } print " $ip = $rdns\n"; print " $rdns = $forward\n"; print " $text_status\n"; } else { print color(160); print " $ip has no reverse DNS\n"; print " " . $status->{bad} . "\n"; } print color(); # If it's NOT the last entry, add an extra \n if ($count != $ip_count) { print "\n"; } $count++; } ###################################################################### sub rdns { my $ip = shift(); my @parts = split(/\./,$ip); @parts = reverse(@parts); my $str = join(".",@parts) . ".in-addr.arpa"; my $query = $res->search($str,'PTR'); my @res; foreach my $rr ($query->answer) { my $addr = $rr->ptrdname; push(@res,$addr); } my $ret = $res[0]; return $ret; } sub resolve { my $domain = shift(); my $type = shift(); $type ||= "A"; my $query = $res->search($domain,$type); my @ip; foreach my $rr ($query->answer) { my $type = $rr->type; if (uc($type) eq 'A') { my $addr = $rr->address; push(@ip,$addr); } elsif (uc($type) eq 'PTR') { print "CNAME: " . $query->name . "\n"; #push(@ip,resolve($query->name)); } } return $ip[0]; } # String format: '115', '165bold', '10_on_140', 'reset', 'on_173' sub color { my ($fc,$bc,$bold,$ret,$str) = ''; $str = shift(); # No string sent in, so we just reset if (!$str || $str =~ /reset/i) { return "\e[0m"; } # Get foreground and bold ($fc,$bold) = $str =~ /^(\d+)(b|bold)?/g; # Get the background color (if present) ($bc) = $str =~ /on_?(\d+)$/g; if ($bold) { $ret .= "\e[1m"; } if ($fc) { $ret .= "\e[38;5;${fc}m"; } if ($bc) { $ret .= "\e[48;5;${bc}m"; } return $ret; } sub usage { return "$0 65.182.224.1 [8.8.8.8] [4.2.2.1]\n"; }