Skip to content

Instantly share code, notes, and snippets.

@unacceptable
Last active November 16, 2023 14:02
Show Gist options
  • Save unacceptable/e51f357c18335e873e8a3dd951264e9b to your computer and use it in GitHub Desktop.
Save unacceptable/e51f357c18335e873e8a3dd951264e9b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
watch_dns(){
HOST=$1
DNS_SERVER=${2:-"8.8.8.8"}
OLD_IP="$(dig +short "$HOST")"
echo "$(date) Starting to watch $HOST for changes from \"$OLD_IP\" as tracked by ${DNS_SERVER}"
while true; do
DNS_LATENCY_PRE_CHECK=$(ping -c 1 "$DNS_SERVER" | ggrep -P '\d+\sbytes' | ggrep -Pow '\d+\.\d+\sms')
IP="$(dig "@${DNS_SERVER}" +short "$HOST")"
DNS_LATENCY_POST_CHECK=$(ping -c 1 "$DNS_SERVER" | ggrep -P '\d+\sbytes' | ggrep -Pow '\d+\.\d+\sms')
if [[ $IP == "$OLD_IP" ]]; then
sleep 1
continue
fi
echo "$(date) IP from \"$OLD_IP\" to \"$IP\", DNS latency before check: $DNS_LATENCY_PRE_CHECK, DNS latency post check: $DNS_LATENCY_POST_CHECK"
OLD_IP=$IP
sleep 1
done
}
watch_dns "$1" "${2:-"8.8.8.8"}"
# example call:
# watch_dns dynamodb.us-east-2.amazonaws.com
# or
# watch_dns google.com 1.1.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment