Created
June 20, 2018 08:55
-
-
Save pulecp/ae527932094e58d5b130e42cfce3c10f to your computer and use it in GitHub Desktop.
Script which checks if A and PTR records match in your zone files of bind server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
data_dir=/var/named/chroot/var/named/data/ | |
[[ -d $data_dir ]] || { echo 'Run me on master bind server, exiting'; exit 1; } | |
echo '###############################################' | |
echo 'Checking if all IP addresses have a PTR record' | |
echo 'and if that PTR matches with the A record' | |
echo '###############################################' | |
echo | |
ips=$(grep -roE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" $data_dir | cut -d ':' -f2) | |
for ip in $ips;do | |
# skip localhost | |
[[ "$ip" =~ 127\..* ]] && continue | |
# find PTR | |
ptr_record=$(dig +short -x $ip) | |
# skip root servers | |
[[ "$ptr_record" =~ .*root-servers\.net\. ]] && continue | |
# find A record of PTR | |
a_record=$(dig +short $ptr_record) | |
# skip IP addreeses with correct reverse record | |
[[ "$ip" == "$a_record" ]] && continue | |
echo "ip: $ip, ptr_record: $ptr_record" | |
done | |
echo | |
echo | |
echo '#################################################' | |
echo 'Checking if all PTR records have correct A record' | |
echo '#################################################' | |
echo | |
ptrs=$(grep -r PTR $data_dir | awk '{ print $4 }') | |
for ptr in $ptrs;do | |
# skip localhost | |
[[ "$ptr" == 'localhost.' ]] && continue | |
# find A record of PTR | |
a_record=$(dig +short $ptr) | |
[[ "$a_record" == '' ]] && { echo "ptr: $ptr - missing A record"; continue; } | |
# find PTR of A record | |
ptr_record=$(dig +short -x $a_record) | |
# skip IP addreeses with correct A record | |
[[ "$ptr" == "$ptr_record" ]] && continue | |
echo "ptr: $ptr, real_ptr: $ptr_record" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment