Advertisement
Guest User

duckDNS_updater_v3.0RC2

a guest
May 25th, 2014
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. # Dependencies:
  3. #  Required:
  4. #   ifconfig && grep && wc && awk && ( curl || wget )
  5. #  Recommended:
  6. #   stat ca-certificates
  7. # Notes:
  8. #  Change eth0 to the correct interface, see ifconfig output for details, if you are behind a router and dont care to look it up "lo" should work
  9.  
  10. # Basic Configuration Options:
  11. DOMAIN="YOUR_SUB-DOMAIN_HERE"
  12. TOKEN="YOUR_TOKEN_HERE"
  13. LOG="/tmp/duckdns"
  14. NIC="eth0"
  15. DEBUG="off"
  16.  
  17. # Advanced Configuration Options:
  18. IP="$LOG.log"
  19. TIME="$LOG-time.log"
  20. WARN="$LOG-warn.log"
  21. ERROR="$LOG-warn.log"
  22.  
  23. # NEW=$(hostname -I) # If this works (and gets the right address) use this, some systems may use -i
  24.  
  25. # You should not need to edit anything below this line
  26.  
  27. if [ -z "$NEW" ];then
  28.     NEW=$(/sbin/ifconfig $NIC | awk -F':' '/inet addr/{split($2,_," ");print _[1]}')
  29. fi
  30. if [ $(echo $NEW | grep '^192.168.\|^10.\|^127.\|^172.1[6-9].\|^172.2[0-9].\|^172.3[0-1].\|^169.254.[1-2]' | wc -l) -eq 1 ];then
  31.     # Match IP range:     192.168.   10.   127.      -->       172.(16-31).      <--        169.254.(1,2)
  32.     echo "Using external IP check, use 11 second intervals or more to avoid being blocked" >> "$WARN"
  33.     if [ $(type curl > /dev/null;echo $?) -eq 0 ];then
  34.         NEW=$(curl http://ipecho.net/plain 2>/dev/null)
  35.     else
  36.         NEW=$(wget -qO- http://ipecho.net/plain)
  37.     fi
  38.     if [ $? -ne 0 ];then
  39.         echo "Failed to get external IP" >> "$ERROR"
  40.         exit
  41.     fi
  42. fi
  43. TAT=1
  44. if [ $(type stat > /dev/null;echo $?) -eq 0 ];then
  45.     TAT=0;
  46. fi
  47. if [ -n "$NEW" ];then
  48.     if [ -f "$IP" ];then
  49.         if [ $TAT -eq 0 ];then
  50.             THEN=$(stat -c "%Y" "$IP")
  51.         else
  52.             THEN=$(cat "$TIME")
  53.         fi
  54.         OLD=$(cat "$IP")
  55.     else
  56.         THEN=864000
  57.         OLD="0.0.0.0"
  58.     fi
  59.     NOW=$(date +%s)
  60.     if [ "$OLD" != "$NEW" ] || [ $(($NOW-$THEN)) -gt 863999 ];then
  61.         S="https://"
  62.         D="http://"
  63.         URL="www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$NEW"
  64.         if [ $DEBUG != "off" ];then
  65.             echo $URL
  66.         fi
  67.         if [ $(type curl > /dev/null;echo $?) -eq 0 ];then
  68.             NOW=$(curl "$S$URL" 2>/dev/null)
  69.             if [ $? -eq 77 ];then
  70.                 echo "Looks like ca-certificates is missing or outdated" >> "$WARN"
  71.                 NOW=$(curl -k "$S$URL" 2>/dev/null)
  72.             fi
  73.         else
  74.             NOW=$(wget -q -O - "$S$@" "$URL")
  75.             if  [ $? -eq 1 ];then
  76.                 if [ $(wget --help|grep '--no-check-certificate') ];then
  77.                     echo "Looks like ca-certificates is missing or outdated" >> "$WARN"
  78.                     NOW=$(wget --no-check-certificate -q -O - "$@" "$S$URL")
  79.                 else
  80.                     echo "wget does not appear to support SSL" >> "$WARN"
  81.                     NOW=$(wget -q -O - "$@" "$D$URL")
  82.                 fi
  83.             fi
  84.         fi
  85.         if [ "$NOW" = "OK" ]; then
  86.             echo "$NEW" > "$IP"
  87.             if [ $TAT -eq 1 ];then
  88.                 echo "$(date +%s)" > "$TIME"
  89.             fi
  90.         elif [ "$NOW" = "KO" ]; then
  91.             echo "Double-check your Token/Domain" >> "$ERROR"
  92.         else
  93.             echo "$NOW" >> "$ERROR"
  94.         fi
  95.     fi
  96. fi
  97. if [ $DEBUG != "off" ];then
  98.     if [ -f "$ERROR" ];then
  99.         ls -l "$ERROR"
  100.         echo "Error Log:"
  101.         cat "$ERROR"
  102.         echo '--------------------'
  103.     fi
  104.     if [ -f "$WARN" ];then
  105.         ls -l "$WARN"
  106.         echo "Warning Log:"
  107.         cat "$WARN"
  108.         echo '--------------------'
  109.     fi
  110.     if [ -f "$IP" ];then
  111.         ls -l "$IP"
  112.         echo "Last IP:"
  113.         cat "$IP"
  114.     else
  115.         echo "NO last IP, well this is not good"
  116.     fi
  117. else
  118.     if [ $(cat "$ERROR" | wc -l) -gt 16 ];then
  119.         cat "$ERROR" | tail -8 > "$ERROR"
  120.     fi
  121.     if [ $(cat "$WARN" | wc -l) -gt 16 ];then
  122.         cat "$WARN" | tail -8 > "$WARN"
  123.     fi
  124. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement