Skip to content

Instantly share code, notes, and snippets.

@mikhailbot
Created May 3, 2022 23:49
Show Gist options
  • Save mikhailbot/cc0120ebead8f0dc5d96d8faa99f9320 to your computer and use it in GitHub Desktop.
Save mikhailbot/cc0120ebead8f0dc5d96d8faa99f9320 to your computer and use it in GitHub Desktop.
Simple quick script to run to alert me if the IP address of a DNS record has changed
#!/bin/bash
# This script requires 3 environment variables defined:
# IP_CHANGE_DOMAIN the domain to check for an IP address change
# PUSHOVER_TOKEN the application token for PushOver
# PUSHOVER_USER the user key to send notification to
echo $(date) "Checking IP of $IP_CHANGE_DOMAIN from DNS..."
DOMAIN_IP=$(dig +short ${IP_CHANGE_DOMAIN})
echo $(date) $DOMAIN_IP
echo $(date) "Checking IP of host running this script..."
IP=$(curl -s https://icanhazip.com)
echo $(date) $IP
if [ $DOMAIN_IP == $IP ]
then
echo $(date) "IP addresses match, nothing to report..."
else
echo $(date) "IP addresses do not match, sending PushOver notification..."
RESULT=$(curl -s --request POST \
--url https://api.pushover.net/1/messages.json \
--header 'Content-Type: application/json' \
--data '{
"token": "'$PUSHOVER_TOKEN'",
"user": "'$PUSHOVER_USER'",
"message": "The IP address for '$IP_CHANGE_DOMAIN' has changed, it is now '$IP'"
}')
echo $(date) $RESULT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment