#!/bin/bash

#delcare array badips and accumulate values to array elemenrs
badips=( $( cat /usr/local/nagios/dnsbllisted.txt | grep NAME |uniq | while read revdns
do
        chkrevdns=`echo $revdns | awk '{print $4}'`
        ip=`echo $revdns | awk '{print $2}'`
        if [ "$chkrevdns" = '---' ]; then
                echo no-rev-dns-$ip
        elif [ "$( grep $ip /usr/local/nagios/fwd_dns_whitelist_ips)" ]; then
                echo ""
        else
                revdomain=`echo $chkrevdns | sed 's/\.$//'`
                forwardip=`host $revdomain  | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'`
                if [ ! "$forwardip" = "$ip" ]; then
                        echo "rev-fwd-err-for--$ip---$revdomain"
                fi
        fi
done ) )

#print all array elements with --- in between
badips=( "${badips[@]/%/ --- }" )
if [ "$( echo "${badips[@]}")" ]; then
        echo "WARNING: Mail IPs/hostnames that dont have reverse/forward dns: ${badips[@]}"
        exit 1
else
        echo "OK: All mail IPs has got reverse/forward dns"
fi
