Skip to content

Instantly share code, notes, and snippets.

@smilingpoplar
Created April 30, 2019 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smilingpoplar/16070960bc20507440df0271767ca1f3 to your computer and use it in GitHub Desktop.
Save smilingpoplar/16070960bc20507440df0271767ca1f3 to your computer and use it in GitHub Desktop.
ddns for dnspod
#!/bin/sh
token='<dnspod_id>,<dnspod_token>'
domain='<your_domain>'
sub_domain='<your_subdomain>'
new_ip=${3:-$(curl -s 'https://api.ip.sb/geoip/' | sed 's/.*"ip":"\([0-9.]*\)".*/\1/')}
api_call() {
local param="login_token=${token}&format=json&domain=${domain}&${2}"
curl -s -k -XPOST -o- -A'shdns/0.1(php@html.js.cn)' -d $param https://dnsapi.cn/${1}
}
api_error() {
local key='"code":"1",'
[ "${1#*$key}" = "$1" ] && {
echo "error: $2"
echo $1
exit 1
}
}
(echo "$new_ip" | grep -Eq "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$") || {
echo "Invalid IP: ${new_ip}"
exit 1
}
record=$(api_call "Record.List" "sub_domain=${sub_domain}")
api_error "$record" "Unable to find the record."
old_ip=$(echo "$record" | sed 's/.*"value":"\([0-9.]*\)".*/\1/')
[ "$old_ip" = "$new_ip" ] && {
echo "Record(${new_ip}) not changed. Skipped."
exit 0
}
echo "Changing A record(${sub_domain}.${domain}) from ${old_ip} to ${new_ip} ..."
record_id=$(echo "$record" | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
result=$(api_call "Record.Ddns" "record_id=${record_id}&sub_domain=${sub_domain}&record_type=A&value=${new_ip}&record_line=%E9%BB%98%E8%AE%A4")
api_error "$result" "Failed to update recode."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment