OpenWrt DDNS Scripts For DNSPod

使用方法:

在/etc/ddns/services末尾添加一行:

"DNSPod.cn"		"update_dnspod.sh"

创建/usr/lib/ddns/update_dnspod.sh:

#!/bin/sh
##############################
# DNSPod ddns-scripts v0.1
# By F-Ever
# https://f-ever.cn/
# 2019-01-19 23:00
##############################

. /usr/share/libubox/jshn.sh
api_url_ddns='https://dnsapi.cn/Record.Ddns'
api_url_record_list='https://dnsapi.cn/Record.List'
api_url_record_create='https://dnsapi.cn/Record.Create'

domain_full=$domain
login_token=$username','$password
sub_domain=$(echo $domain_full | awk -F '.' '{print $1}')
domain=$(echo $domain_full | sed s#$sub_domain\\.##g)

get_record_id() {
	record_list_json=$(curl -sX POST $api_url_record_list -d "login_token=${login_token}&format=json&domain=${domain}&sub_domain=${sub_domain}&record_type=A&offset=0&length=3")
	json_cleanup
	json_load "$record_list_json"
	json_select status
	json_get_var var_code code
	json_get_var var_message message
	case $var_code in
		1 )
			json_select ..
			json_select records
			json_select 1
			json_get_var record_id "id"
			write_log 7 "subdomain $sub_domain already exist"
			return 0
			;;
		10 )
			write_log 7 "subdomain $sub_domain not exist"
			return 1
			;;
		* )
			write_log 14 "$var_message"
			exit 1
			;;
	esac
}

record_update() {
	record_update_json=$(curl -sX POST $api_url_ddns -d "login_token=${login_token}&format=json&domain=${domain}&sub_domain=${sub_domain}&record_id=${record_id}&value=${__IP}&record_line=%e9%bb%98%e8%ae%a4")
	json_cleanup
	json_load "$record_update_json"
	json_select status
	json_get_var var_code code
	json_get_var var_message message
	case $var_code in
		1 )
			write_log 7 "$var_message"
			return 0
			;;
		* )
			write_log 14 "$var_message"
			exit 1
			;;
	esac
}

record_add() {
	record_add_json=$(curl -sX POST $api_url_record_create -d "login_token=${login_token}&format=json&domain=${domain}&sub_domain=${sub_domain}&record_type=A&value=${__IP}&record_line=%e9%bb%98%e8%ae%a4")
	json_cleanup
	json_load "$record_add_json"
	json_select status
	json_get_var var_code code
	json_get_var var_message message
	case $var_code in
		1 )
			write_log 7 "$var_message"
			return 0
			;;
		* )
			write_log 14 "$var_message"
			exit 1
			;;
	esac
}

get_record_id
case $? in
	0 )
		write_log 7 "Update Record of $domain_full"
		record_update
		;;
	1 )
		write_log 7 "Add Record of $domain_full"
		record_add
		;;
esac

return 0

发表评论

电子邮件地址不会被公开。