2024-02-28 14:07:09 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "Usage: $0 -i <IP checker service> -d <domain to resolve> -r <DNS server to query>"
|
2024-02-28 14:13:33 -07:00
|
|
|
exit 3
|
2024-02-28 14:07:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
IP_CHECKER=""
|
|
|
|
DOMAIN=""
|
|
|
|
DNS_SERVER=""
|
|
|
|
|
|
|
|
while getopts "i:d:r:" opt; do
|
|
|
|
case ${opt} in
|
|
|
|
i )
|
|
|
|
IP_CHECKER=$OPTARG
|
|
|
|
;;
|
|
|
|
d )
|
|
|
|
DOMAIN=$OPTARG
|
|
|
|
;;
|
|
|
|
r )
|
|
|
|
DNS_SERVER=$OPTARG
|
|
|
|
;;
|
|
|
|
\? )
|
|
|
|
echo "Invalid option: $OPTARG" 1>&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
: )
|
|
|
|
echo "Invalid option: $OPTARG requires an argument" 1>&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND -1))
|
|
|
|
|
|
|
|
if [ -z "$IP_CHECKER" ] || [ -z "$DOMAIN" ] || [ -z "$DNS_SERVER" ]; then
|
|
|
|
echo "All parameters are required"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
CURRENT_IP=$(curl -s "$IP_CHECKER")
|
|
|
|
RESOLVED_IP=$(dig +short @$DNS_SERVER $DOMAIN | head -n 1)
|
|
|
|
|
|
|
|
if [ "$CURRENT_IP" == "$RESOLVED_IP" ]; then
|
|
|
|
echo "OK - $DOMAIN resolves to $CURRENT_IP"
|
2024-02-28 14:16:33 -07:00
|
|
|
exit 0
|
2024-02-28 14:07:09 -07:00
|
|
|
else
|
|
|
|
echo "CRITICAL - $DOMAIN does not resolve to $CURRENT_IP. Resolved: $RESOLVED_IP"
|
|
|
|
exit 2
|
|
|
|
fi
|