RandomUsefulStuff/goip_dyndns_updater.sh

62 lines
2.0 KiB
Bash
Raw Normal View History

2021-08-13 13:58:49 +02:00
#!/bin/bash
################################################################################
# CONFIGURATION
################################################################################
# check every TIMEOUT seconds (default 300 = 5 min)
TIMEOUT=300
# user credentials
USERNAME="ur goip username"
PASSWORD="ur goip password"
DOMAIN="urdomain.goip.it"
# Discord webhook
DISCORD_WEBHOOK_URL="" # leave empty to disable
DISCORD_WEBHOOK_MESSAGE_UPDATE="DNS entry for $DOMAIN has been updated successfully. ($(date))"
DISCORD_WEBHOOK_MESSAGE_FAIL="DNS entry update for $DOMAIN failed. ($(date))"
2021-08-13 13:58:49 +02:00
################################################################################
# END OF CONFIGURATION
################################################################################
function discord_update_message {
if [ -n $DISCORD_WEBHOOK_URL ]; then
local MESSAGE='{"username": "DNS Record Updater", "content": "'"$1"'"}'
wget -qO - --post-data="$MESSAGE" --header='Content-Type:application/json' "$DISCORD_WEBHOOK_URL"
fi
}
function get_ip {
# get a string in format 'ip=x.x.x.x' by splitting out a json over multiple lines and removing characters
local IP=$(wget -4qO - "https://api.myip.com" | sed 's/[\{\}]//g;s/,/\n/g' | sed -n 's/"//g;s/:/=/g;/ip/p')
if [ -z $IP ]; then
echo "Getting my own IP failed." >&2
echo "ip=0.0.0.0"
else
echo $IP
fi
}
function set_ip {
wget -qO - "https://goip.it/setip?username=$USERNAME&password=$PASSWORD&subdomain=$DOMAIN&$1"
local EXIT_CODE="$?"
echo "" # add newline after wget output
if [ $EXIT_CODE -ne 0 ]; then
echo "Setting a new IP using wget failed with exit code $EXIT_CODE." >&2
discord_update_message "$DISCORD_WEBHOOK_MESSAGE_FAIL"
else
discord_update_message "$DISCORD_WEBHOOK_MESSAGE_UPDATE"
fi
}
IP="ip=0.0.0.0"
while true; do
NEW_IP=$(get_ip)
if [ $IP != $NEW_IP ]; then
echo -e "IP has changed. New IP:\n$NEW_IP"
IP="$NEW_IP"
set_ip "$IP"
fi
sleep $TIMEOUT
done