Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Dependencies:
- # Required:
- # ifconfig && grep && wc && awk && ( curl || wget )
- # Recommended:
- # stat ca-certificates
- # Notes:
- # 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
- # Basic Configuration Options:
- DOMAIN="YOUR_SUB-DOMAIN_HERE"
- TOKEN="YOUR_TOKEN_HERE"
- LOG="/tmp/duckdns"
- NIC="eth0"
- DEBUG="off"
- # Advanced Configuration Options:
- IP="$LOG.log"
- TIME="$LOG-time.log"
- WARN="$LOG-warn.log"
- ERROR="$LOG-warn.log"
- # NEW=$(hostname -I) # If this works (and gets the right address) use this, some systems may use -i
- # You should not need to edit anything below this line
- if [ -z "$NEW" ];then
- NEW=$(/sbin/ifconfig $NIC | awk -F':' '/inet addr/{split($2,_," ");print _[1]}')
- fi
- 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
- # Match IP range: 192.168. 10. 127. --> 172.(16-31). <-- 169.254.(1,2)
- echo "Using external IP check, use 11 second intervals or more to avoid being blocked" >> "$WARN"
- if [ $(type curl > /dev/null;echo $?) -eq 0 ];then
- NEW=$(curl http://ipecho.net/plain 2>/dev/null)
- else
- NEW=$(wget -qO- http://ipecho.net/plain)
- fi
- if [ $? -ne 0 ];then
- echo "Failed to get external IP" >> "$ERROR"
- exit
- fi
- fi
- TAT=1
- if [ $(type stat > /dev/null;echo $?) -eq 0 ];then
- TAT=0;
- fi
- if [ -n "$NEW" ];then
- if [ -f "$IP" ];then
- if [ $TAT -eq 0 ];then
- THEN=$(stat -c "%Y" "$IP")
- else
- THEN=$(cat "$TIME")
- fi
- OLD=$(cat "$IP")
- else
- THEN=864000
- OLD="0.0.0.0"
- fi
- NOW=$(date +%s)
- if [ "$OLD" != "$NEW" ] || [ $(($NOW-$THEN)) -gt 863999 ];then
- S="https://"
- D="http://"
- URL="www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$NEW"
- if [ $DEBUG != "off" ];then
- echo $URL
- fi
- if [ $(type curl > /dev/null;echo $?) -eq 0 ];then
- NOW=$(curl "$S$URL" 2>/dev/null)
- if [ $? -eq 77 ];then
- echo "Looks like ca-certificates is missing or outdated" >> "$WARN"
- NOW=$(curl -k "$S$URL" 2>/dev/null)
- fi
- else
- NOW=$(wget -q -O - "$S$@" "$URL")
- if [ $? -eq 1 ];then
- if [ $(wget --help|grep '--no-check-certificate') ];then
- echo "Looks like ca-certificates is missing or outdated" >> "$WARN"
- NOW=$(wget --no-check-certificate -q -O - "$@" "$S$URL")
- else
- echo "wget does not appear to support SSL" >> "$WARN"
- NOW=$(wget -q -O - "$@" "$D$URL")
- fi
- fi
- fi
- if [ "$NOW" = "OK" ]; then
- echo "$NEW" > "$IP"
- if [ $TAT -eq 1 ];then
- echo "$(date +%s)" > "$TIME"
- fi
- elif [ "$NOW" = "KO" ]; then
- echo "Double-check your Token/Domain" >> "$ERROR"
- else
- echo "$NOW" >> "$ERROR"
- fi
- fi
- fi
- if [ $DEBUG != "off" ];then
- if [ -f "$ERROR" ];then
- ls -l "$ERROR"
- echo "Error Log:"
- cat "$ERROR"
- echo '--------------------'
- fi
- if [ -f "$WARN" ];then
- ls -l "$WARN"
- echo "Warning Log:"
- cat "$WARN"
- echo '--------------------'
- fi
- if [ -f "$IP" ];then
- ls -l "$IP"
- echo "Last IP:"
- cat "$IP"
- else
- echo "NO last IP, well this is not good"
- fi
- else
- if [ $(cat "$ERROR" | wc -l) -gt 16 ];then
- cat "$ERROR" | tail -8 > "$ERROR"
- fi
- if [ $(cat "$WARN" | wc -l) -gt 16 ];then
- cat "$WARN" | tail -8 > "$WARN"
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement