Skip to content

Instantly share code, notes, and snippets.

@v-zhuravlev
Last active May 14, 2019 08:02
Show Gist options
  • Save v-zhuravlev/895850ae4fbcd374360ecf82f5068d34 to your computer and use it in GitHub Desktop.
Save v-zhuravlev/895850ae4fbcd374360ecf82f5068d34 to your computer and use it in GitHub Desktop.
#!/bin/bash
## Performs rDNS check using 'host' command
if [[ $# -eq 0 ]] ; then
echo 'You must provide DNS record as the first argument'
exit 0
fi
function lookup(){
OUTPUT=$(host -t A $1);
if [ $? == 0 ]
then
echo $OUTPUT | cut -d " " -f 4
return 0
else
#Print output with error and exit
echo $OUTPUT
return 1
fi
}
function rlookup(){
OUTPUT=$(host -t PTR $1)
if [ $? == 0 ]
then
#sed trims out root domain '.' at the end
echo $OUTPUT | cut -d " " -f 5 | sed s/\.$//
return 0
else
#Print output with error and exit
echo $OUTPUT
return 1
fi
}
OUTPUT=$(lookup $1)
if [ $? == 0 ]
then
OUTPUT=$(rlookup $OUTPUT)
if [ $? == 0 ]
then
if [ $OUTPUT == $1 ];
then
echo "PASSED"
exit 0
else
echo "rDNS check FAILED: '$OUTPUT' doesn't match expected '$1'"
exit 2
fi
else
echo "rDNS check FAILED:" $OUTPUT
exit 2
fi
else
# Print output with error and exit
echo "rDNS check FAILED:" $OUTPUT
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment