Last active
June 21, 2024 01:31
-
-
Save m4rcu5nl/56bcdaa10626210cf18c to your computer and use it in GitHub Desktop.
Simple Bash DNS checker. Intended for bulk lookup by feeding it a list of hostnames.
This file contains 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 | |
# Does a host lookup of specified record type for given hostname(s). | |
# Accepts single hostnames or a file containing a list of hostnames. | |
# The optional 3rd parameter (expected value) gives a colored output | |
# for fast and easy visual analyzing. | |
# Usage: ./dnscheck [RECORD TYPE] [INPUT(FILE)] ('expected value') | |
EXPECTED="$3" | |
function main { | |
if [[ ! "$1" ]] & [[ ! "$2" ]]; then | |
echo -e "\e[1;31mI need something to work with...\e[0m" | |
echo "Usage: ./dnscheck.sh [RECORD_TYPE] [INPUT] (expected value)" | |
else | |
if [[ -f "$2" ]]; then | |
target=`cat "$2"` | |
else | |
target="$2" | |
fi | |
case $1 in | |
"A" ) getA "$target" ;; | |
"NS" ) getNS "$target" ;; | |
"MX" ) getMX "$target" ;; | |
* ) echo "Not a valid record" ;; | |
esac | |
fi | |
} | |
function getNS { | |
for i in $1; | |
do | |
domain=`echo "$i" |rev |cut -d'.' -f 1,2 |rev` | |
nameservers=`host -t NS $domain |awk '{print $4}'` | |
if [[ ! "$EXPECTED" ]]; then | |
echo -e "\e[1m$domain:\e[0m\n$nameservers" | |
elif [[ $nameservers != *$EXPECTED* ]]; then | |
echo -e "\e[1;31m$domain\e[0m:\n$nameservers" | |
else | |
echo -e "\e[1;32m$domain\e[0m:\n$nameservers" | |
fi | |
done | |
} | |
function getA { | |
for i in $1; | |
do | |
ip_address=`host -t A $i |grep 'has address' |awk '{print $4}'` | |
if [[ ! "$EXPECTED" ]]; then | |
echo -e "\e[1m$i\e[0m: $ip_address" | |
elif [[ $ip_address != *$EXPECTED* ]]; then | |
echo -e "\e[1;31m$i\e[0m: $ip_address" | |
else | |
echo -e "\e[32m$i\e[0m: $ip_address" | |
fi | |
done | |
} | |
function getMX { | |
for i in $1; | |
do | |
domain=`echo "$i" |rev |cut -d'.' -f 1,2 |rev` | |
mxrecords_prio=`host -t MX $domain |grep 'handled by' |awk '{print $7,$6}'` | |
if [[ ! "$EXPECTED" ]]; then | |
echo -e "\e[1m$i\e[0m:\n$mxrecords_prio" | |
elif [[ $mxrecords_prio != *$EXPECTED* ]]; then | |
echo -e "\e[1;31m$i\e[0m:\n$mxrecords_prio" | |
else | |
echo -e "\e[32m$i\e[0m:\n$mxrecords_prio" | |
fi | |
done | |
} | |
main $1 $2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment