Skip to content

Instantly share code, notes, and snippets.

@gerlof85
Last active May 17, 2021 13:22
Show Gist options
  • Save gerlof85/ed4f3450883e69191492f57ddfa6dd65 to your computer and use it in GitHub Desktop.
Save gerlof85/ed4f3450883e69191492f57ddfa6dd65 to your computer and use it in GitHub Desktop.
updated twodns updater, which only calls the API if current external ip is different from the one stored in current_ip.txt
# !/bin/sh
path="/usr/local/bin/twodns/"
# create file if it doesn't exist
if ! test -f $path"current_ip.txt"; then
echo "empty" > $path"current_ip.txt"
fi
ip_address=`cat /usr/local/bin/twodns/current_ip.txt`
my_ext_ip=$(curl -sS http://ipv4.icanhazip.com)
log=$path"ddns_updater.log.txt"
# echo "n$(date -u) IP in file " $ip_address " external IP " $my_ext_ip >> $log
if [ $ip_address = $my_ext_ip ]
then
echo "\n$(date) ip adresses are equal, not updating" >> $log
else
# update ip in file and online
echo "$my_ext_ip" > $path"current_ip.txt"
echo "\n\n\n===== \
\n$(date) \
\nUpdating IP to: $my_ext_ip \n" >> $log
curl -isSX PUT \
-u "[email protected]:YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"ip_address":"'$my_ext_ip'", "activate_wildcard": "false"}' \
https://api.twodns.de/hosts/YOUR_HOSTNAME.dynvpn.de >> $log
exit
fi
@gerlof85
Copy link
Author

this script is based on https://gist.github.com/oliveratgithub/a50a3d6bcb1e030ed06e

changes

  • updated URL for obtaining ipv4 address (old URL doesn't work anymore)
  • highlighted the areas where user data needs to be placed
  • added check for previous IP address, not updating to TwoDNS if current public IP equals stored IP
  • check if current_ip.txt exists, create the file if it doesn't exist

@gerlof85
Copy link
Author

gerlof85 commented May 17, 2021

  • added path reference for improved file/log handling. Note: the path needs to be updated in 2 lines in case the script is used in a different location (this needs to be improved)
  • remove -u after date calls to make log timestamps in sync with current timezone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment