Gather IP address of a computer and push it to an Snap DynDNS Server instance.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dnsupdate.sh 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. ########################################################################
  3. # Copyright (C) 2016 Max Mehl <mail [at] mehl [dot] mx>
  4. ########################################################################
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ########################################################################
  20. #
  21. # Client part of the "Snap DynDNS Server", grabbing public IP address
  22. # and pushing it to the server handling the more complicated API stuff
  23. #
  24. ########################################################################
  25. cd "$(dirname "$(readlink -f "$0")")"
  26. # Test if config.cfg exists and set needed variables
  27. if [ ! -e config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
  28. source config.cfg
  29. # Test whether necessary applications are installed
  30. function testinst {
  31. APP=$1
  32. if [ $(which $APP | wc -l) != 1 ]; then
  33. echo "$APP does not seem to be installed. Aborting."
  34. exit 1
  35. fi
  36. }
  37. testinst curl
  38. testinst wget
  39. # Does the old.ip file exist? If not, fill it with unrealistic input
  40. if [ ! -e old.ip ]; then
  41. echo "0.0.0.0" > old.ip
  42. fi
  43. # Read old IP, gather new IP via IP return service
  44. OLDIP=$(cat old.ip)
  45. NEWIP=$(wget -T $TIMEOUT -q -O - $IPSERV)
  46. # Post current timestamp in logfile
  47. echo "[$(date "+%Y-%m-%d %H:%M:%S")]" > update.log
  48. # check whether the IP has changed since the last update
  49. if [ "$OLDIP" == "$NEWIP" ]; then
  50. echo "IP ($OLDIP) did not change. Aborting." >> update.log
  51. exit 0
  52. fi
  53. # Check whether IP is on ignore list
  54. if [ $(echo "$IGNOREIP" | grep "$NEWIP") ]; then
  55. echo "Current IP ($NEWIP) is ignored. Aborting without DNS update." >> update.log
  56. exit 0
  57. fi
  58. # Check whether user is using a Tor exit node IP
  59. if [ "$CHECKTOR" = true ]; then
  60. # Only download list of exit nodes if tor-ips.txt file isn't existing yet, or if it is older than 7 days
  61. if [ ! -e tor-ips.txt ] || [ $[ $(date +%s) - $(stat -L -c %Y tor-ips.txt) ] -gt "604800" ]; then
  62. # download list, extract plain IPs, move back to file
  63. echo "download list"
  64. wget -O tor-ips.txt $EXITNODES
  65. TORIPS=$(grep "^ExitAddress" tor-ips.txt | cut -d" " -f2)
  66. echo $TORIPS | sed 's/ /\n/g' > tor-ips.txt
  67. fi
  68. # Grep current IP in list of exit node IPs
  69. if [ $(grep "^$NEWIP$" tor-ips.txt) ]; then
  70. echo "Current IP ($NEWIP) is ignored because it is a Tor exit IP. Aborting without DNS update." >> update.log
  71. exit 0
  72. fi
  73. fi
  74. # Run update if old IP and current IP are not equal
  75. if [ ! "$OLDIP" == "$NEWIP" ]; then
  76. echo $NEWIP > old.ip
  77. curl -X POST -d "domain=$DOMAIN&user=$USER&pass=$PASS&ip=$NEWIP" "$SERVER" >> update.log
  78. echo "" >> update.log
  79. echo "Updated IP address from $OLDIP to $NEWIP." >> update.log
  80. fi