Skip to content

Instantly share code, notes, and snippets.

@vincascm
Created August 26, 2012 10:35
Show Gist options
  • Select an option

  • Save vincascm/3477105 to your computer and use it in GitHub Desktop.

Select an option

Save vincascm/3477105 to your computer and use it in GitHub Desktop.
dnspod ddns client for Openwrt
#!/bin/sh
# sddns, a dnspod ddns client for OpenWrt
# Written by vinoca, June 2012
# Distributed under the MIT License
#
# more visit www.vinoca.org
VERSION=0.0.4
[ ! `type -p curl` ] && echo "missing curl utility."&& exit
# load config
#
. /etc/functions.sh
config_cb() {
option_cb() {
eval $1=$2
}
}
config_load 'sddns'
# define routines
#
post() {
curl -s --cacert $cacert -A "sddns/$VERSION([email protected])" -d "login_email=$login_email&login_password=$login_password&format=json&error_on_empty=no" -d "$1" "https://dnsapi.cn/$2"
}
get_domain_id() {
sed -rn 's/.*("domain":\{.[^}]+\}).*/\1/g;s/.*"id":"([0-9]+)".*/\1/p'
}
get_record_id() {
sed -rn "s/.*(\"records\":\[.[^]]+\]).*/\1/g;s/.*\{\"id\":\"([0-9]+)\",\"name\":\"$1\".*\"type\":\"A\".*/\1/p"
}
# test \$wanip
#
[ -z "$wanip" ] && {
echo "ERROR: Please check the \"/etc/config/sddns\" file, \"wanip\" is invalid parameter."
exit
}
cur_wanip=`curl -sL $wanip | sed -rn 's/[^0-9.]*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*$/\1/p'`
[ -z "$cur_wanip" ] && {
echo "WARNING: \"wanip\" is invalid."
ignore_wanip=1
}
# save domain_id and record_id
#
[ -z "$domain_id" ] && {
domain_id=`post "domain=$main_domain" Domain.Info | get_domain_id`
uci set sddns.@tmp_arg[0].domain_id=$domain_id &&uci commit
}
[ -z "$record_id" ] && {
record_id=`post "domain_id=$domain_id" Record.List | get_record_id $sub_domain`
uci set sddns.@tmp_arg[0].record_id=$record_id &&uci commit
}
# save wanip
#
[ -z $ignore_wanip ] && [ -z "$cur_ip" ] && {
uci set sddns.@tmp_arg[0].cur_ip=$cur_wanip &&uci commit
}
# update subdomain record
#
if [ -z $ignore_wanip ]; then
[ "$cur_ip" != "$cur_wanip" ] && {
uci set sddns.@tmp_arg[0].cur_ip=$cur_wanip &&uci commit
post "domain_id=$domain_id&record_id=$record_id&sub_domain=$sub_domain&record_line=默认" Record.Ddns
}
else
post "domain_id=$domain_id&record_id=$record_id&sub_domain=$sub_domain&record_line=默认" Record.Ddns
fi
# vim: set ts=2 sw=2 noet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment