Skip to content

Instantly share code, notes, and snippets.

@dishuostec
Created February 24, 2018 06:26
Show Gist options
  • Save dishuostec/491a75aa9ef8e82df102bddd8e60f921 to your computer and use it in GitHub Desktop.
Save dishuostec/491a75aa9ef8e82df102bddd8e60f921 to your computer and use it in GitHub Desktop.
pure shell dnspod ddns client
#!/bin/sh
HOST=https://dnsapi.cn
USERAGENT='DNSPOD DDNS Client/1.0.0 ([email protected])'
if [ -n "$(which wget)" ];then
CMD=wget
fi
if [ -n "$(which curl)" ];then
CMD=curl
fi
if [ -z "${CMD}" ];then
echo need curl or wget!
exit 1
fi
current_ip() {
local URL=http://greak.net/ip
case "${CMD}" in
curl)
curl --silent ${URL}
;;
wget)
wget --quiet --no-check-certificate --output-document=- ${URL}
;;
esac
}
echo use: ${CMD}
NEW_IP=$(current_ip)
if [ -z "${NEW_IP}" ];then
echo could not get current wan ip.
exit
fi
echo WAN ip: ${NEW_IP}
post() {
URL=$1
DATA=$2
case "${CMD}" in
curl)
curl --silent -X POST ${HOST}${URL} -d "login_token=${TOKEN}&format=json&${DATA}"
;;
wget)
wget --quiet --no-check-certificate --output-document=- --post-data "login_token=${TOKEN}&format=json&${DATA}" ${HOST}${URL}
;;
esac
}
update_ddns() {
DOMAIN=$1
SUB_DOMAIN=$2
echo "${SUB_DOMAIN}.${DOMAIN} -> ${NEW_IP}"
DOMAIN_ID=$(post '/Domain.Info' "domain=${DOMAIN}" | sed 's/.*"id":"\([0-9]*\)".*/\1/')
#echo domain id: ${DOMAIN_ID}
RECORD=$(post '/Record.List' "domain_id=${DOMAIN_ID}&sub_domain=${SUB_DOMAIN}")
CODE=$(echo ${RECORD} | sed 's/.*"code":"\([0-9]*\)".*/\1/')
if [ "${CODE}" = "10" ];then
echo create new record
post '/Record.Create' "domain_id=${DOMAIN_ID}&sub_domain=${SUB_DOMAIN}&value=127.0.0.1&record_type=A&record_line=默认"
RECORD=$(post '/Record.List' "domain_id=${DOMAIN_ID}&sub_domain=${SUB_DOMAIN}")
fi
RECORD_ID=$(echo ${RECORD} | sed 's/.*\[[^]]*"id":"\([0-9]*\)".*/\1/')
#echo record id: ${RECORD_ID}
OLD_IP=$(echo ${RECORD} | sed 's/.*\[[^]]*"value":"\([0-9.]*\)".*/\1/')
echo old ip: ${OLD_IP}
if [ "${NEW_IP}" = "${OLD_IP}" ];then
echo skip
else
post '/Record.Ddns' "domain_id=${DOMAIN_ID}&record_id=${RECORD_ID}&sub_domain=${SUB_DOMAIN}&value=${NEW_IP}&record_line=默认"
fi
}
TOKEN="12345,1234567890abcdef"
update_ddns 'example-domain.com' 'subdomain'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment