Skip to content

Instantly share code, notes, and snippets.

@hiifeng
Created November 9, 2022 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiifeng/ed11c5527c3f55f5c5fdc20e3bd19dad to your computer and use it in GitHub Desktop.
Save hiifeng/ed11c5527c3f55f5c5fdc20e3bd19dad to your computer and use it in GitHub Desktop.
ddns_update.sh
#!/bin/sh
#
#############################################################
# ddnsupdate v1.0.0
#
# Dynamic DNS using DNSPod API && dynv6.com for hs8145v
#
# Author: trotter, https://gist.github.com/hiifeng
#
# Usage: please refer to `ddns_update.sh`
#
#############################################################
dynv6_token="your token"
dynv6_hostname="example.dynv6.net"
dnspod_token="your ID,your token"
dnspod_hostname="example.dnspod.cn"
#Check whether the parameters of dynv6 are complete
if [ -z "$dynv6_token" ] || [ -z "$dynv6_hostname" ]; then
echo "Usage: dynv6_token=\"your-authentication-token\";dynv6_hostname=\"example.dynv6.net\""
exit
fi
#Check whether the parameters of dnspod are complete
if [ -z "$dnspod_token" ] || [ -z "$dnspod_hostname" ]; then
echo "Usage: dnspod_token=\"your-authentication-token\";dnspod_hostname=\"example.dnspod.cn\""
exit
fi
file=$HOME/.ddnsupdate.addr
if [ -e $file ];then
lastIp=`cat $file`
fi
#Get WAN Ip Addresses
current_Ip_v4=$(ifconfig | grep "P-t-P" | sed 's/^.*addr://g' | sed 's/ P-t-P.*$//g')
current_Ip_v6=$(ifconfig | grep "Scope:Global" | sed 's/^.*addr: //g' | sed 's/\/128.*$//g')
if [ -z "$current_Ip_v4" ] || [ -z "$current_Ip_v6" ]; then
echo "Can't get ip address"
exit
fi
if [ "$lastIp" = "$current_Ip_v6" ]; then
echo "IPv6 address unchanged"
exit
fi
if [ -e /usr/bin/wget ]; then
dynv6_bin="wget -q -O-"
dnspod_bin="wget -q -O- --no-check-certificate --post-data"
else
echo "wget not found"
exit
fi
dnspod_domain=${dnspod_hostname#*.}
dnspod_sub_domain=${dnspod_hostname%%.*}
dnspod_search_recordId_params_ipv4="login_token=$dnspod_token&format=json&domain=$dnspod_domain&sub_domain=$dnspod_sub_domain&record_type=A"
dnspod_search_recordId_params_ipv6="login_token=$dnspod_token&format=json&domain=$dnspod_domain&sub_domain=$dnspod_sub_domain&record_type=AAAA"
dnspod_record_list_ipv4=$($dnspod_bin $dnspod_search_recordId_params_ipv4 https://dnsapi.cn/Record.List)
dnspod_record_list_ipv6=$($dnspod_bin $dnspod_search_recordId_params_ipv6 https://dnsapi.cn/Record.List)
dnspod_record_id_ipv4=$(echo $dnspod_record_list_ipv4 | sed 's/.*"id":"\([0-9]*\)".*/\1/')
dnspod_record_id_ipv6=$(echo $dnspod_record_list_ipv6 | sed 's/.*"id":"\([0-9]*\)".*/\1/')
dnspod_update_addresses_params_ipv4="login_token=$dnspod_token&format=json&domain=$dnspod_domain&record_id=$dnspod_record_id_ipv4&sub_domain=$dnspod_sub_domain&record_type=A&record_line_id=0&value=$current_Ip_v4"
dnspod_update_addresses_params_ipv6="login_token=$dnspod_token&format=json&domain=$dnspod_domain&record_id=$dnspod_record_id_ipv6&sub_domain=$dnspod_sub_domain&record_type=AAAA&record_line_id=0&value=$current_Ip_v6"
# Send Ip Addresses to dnspod
$dnspod_bin $dnspod_update_addresses_params_ipv4 https://dnsapi.cn/Record.Modify
$dnspod_bin $dnspod_update_addresses_params_ipv6 https://dnsapi.cn/Record.Modify
# Send Ip Addresses to dynv6
$dynv6_bin "http://dynv6.com/api/update?hostname=$dynv6_hostname&ipv6=$current_Ip_v6/128&token=$dynv6_token"
$dynv6_bin "http://ipv4.dynv6.com/api/update?hostname=$dynv6_hostname&ipv4=$current_Ip_v4&token=$dynv6_token"
# Save current IPv6 address
echo $current_Ip_v6 > $file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment