Skip to content

Instantly share code, notes, and snippets.

@isjerryxiao
Last active February 3, 2017 08:52
Show Gist options
  • Save isjerryxiao/215df9be2b513962c4aedf7d6633f424 to your computer and use it in GitHub Desktop.
Save isjerryxiao/215df9be2b513962c4aedf7d6633f424 to your computer and use it in GitHub Desktop.
重要:已经放弃更新,请使用 https://github.com/SYHGroup/easy_shell/blob/master/ddns/dnspod-ddns.sh ---------------------dnspod.cn服务器ip更新脚本(使用dnspod ddns api,可更改ttl),需要bash,curl,grep,sed,不建议在openwrt使用。使用时修改以下信息:TokenID、Token、ddnspod your domain.com.----------rm dnspod.sh && wget --no-cache https://gist.githubusercontent.com/Jerry981028/215df9be2b513962c4aedf7d…
#!/usr/bin/env bash
#encoding=utf8
function logecho(){
#若不需要记录日志注销下行。
echo "$*" >> ${logfile}
echo "$*"
}
function ddnspod(){
# Usage: ddnspod [subdomain] [domain]
if [ $# == 2 ]
then
SubDomain="$1"
Domain="$2"
logecho "$(date): dnspod update ${SubDomain}.${Domain}."
oip=$(curl -s -X POST https://dnsapi.cn/Record.List -d "login_token=${TokenID},${Token}&format=json&domain=${Domain}&record_id=${RecordID}&sub_domain=${SubDomain}" | grep value | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
if [ "$oip" == "" ] ; then
logecho "Error: Fail to connect to dnspod.cn."
exit 1
fi
nip=$(curl -s http://members.3322.org/dyndns/getip)
if [ "$nip" == "" ] ; then
logecho "Warning: Fail to connect to members.3322.org. Ignore it."
fi
logecho "Old IP: ${oip}"
if [ "$oip" == "$nip" ]
then
logecho "IP hasn't been changed."
else
logecho "New IP: ${nip}"
RecordID=$(curl -s -X POST https://dnsapi.cn/Record.List -d "login_token=${TokenID},${Token}&format=json&domain=${Domain}" | sed -n 's/.*"id":"\(.*\)","name":"'${SubDomain}'".*/\1/p')
Result=$(curl -s -X POST https://dnsapi.cn/Record.Ddns -d "login_token=${TokenID},${Token}&format=json&domain=${Domain}&record_id=${RecordID}&record_line_id=0&sub_domain=${SubDomain}" | sed -n 's/.*"message":"\(.*\)","created_at".*/\1/p')
logecho "DDNS: ${Result}"
oip=$(curl -s -X POST https://dnsapi.cn/Record.List -d "login_token=${TokenID},${Token}&format=json&domain=${Domain}&record_id=${RecordID}&sub_domain=${SubDomain}" | grep value | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
if [ "$oip" == "" ] ; then
logecho "Error: TTL can't be changed."
exit 1
elif [ "$oip" != "$nip" ] ; then
logecho "Warning: IP doesn't match. [dnspod:${oip} , 3322.org:${nip}]"
fi
Result=$(curl -s -X POST https://dnsapi.cn/Record.Modify -d "login_token=${TokenID},${Token}&format=json&domain=${Domain}&record_id=${RecordID}&sub_domain=${SubDomain}&record_line_id=0&value=${oip}&record_type=A&ttl=${Ttl}" | sed -n 's/.*"message":"\(.*\)","created_at".*/\1/p')
logecho "TTL: ${Result}"
fi
else
logecho "Error: function ddnspod: Argument error."
fi
}
function checklogfile(){
lines=$(sed -n '$=' ${logfile})
if (( $lines > 200 )) ; then
if [ -f ${logfile}.1 ] ; then
rm ${logfile}.1
fi
mv $logfile ${logfile}.1
touch $logfile
fi
}
TokenID="*****"
Token="****************"
logfile="/var/log/dnspod-1.log"
Ttl=3600
#### example for your.domain.com ####
ddnspod your domain.com
#若不需要记录日志注销下行。
checklogfile
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment