CloudXNS DDNS on bash
将NS服务商从DNSPod迁移到CloudXNS后,解析速度快了不少,唯独DDNS服务还是一直使用委派一个子域回DNSPod,用DNSPod进行DDNS的方式进行。搜索一番以后,发现没有现成的轮子,因此在研究了CloudXNS的API文档后,决定自己动手写一个原生的CloudXNS DDNS shell。
操作系统要求
- 运行OpenWrt或tomato系统
- 支持curl命令
解决方案
项目地址:https://github.com/kuretru/CloudXNS-DDNS。点此直接下载脚本
#!/bin/sh
#==================================================
# OS Required: Linux with curl
# Description: CloudXNS DDNS on bash
# Author: Kuretru
# Version: 1.1.160913
# Github: https://github.com/kuretru/CloudXNS-DDNS/
#==================================================
#API Key
api_key=""
#Secret Key
secret_key=""
#Domain name
#e.g. domain="www.cloudxns.net."
domain=""
#WAN Network Interface Card
#e.g. interface="" -> If it's empty, CloudXNS will automatic get your public IP
#or interface="ppp0" -> on tomato
#or interface="pppoe-wan1" -> on OpenWRT
interface=""
if [ -n "$interface" ] ;then
value=$(ifconfig $interface|grep 'inet addr'|awk -F ":" '{print $2}'|awk '{print $1}')
fi
url="https://www.cloudxns.net/api2/ddns"
time=$(date -R)
data="{\"domain\":\"${domain}\",\"ip\":\"${value}\",\"line_id\":\"1\"}"
mac_raw="$api_key$url$data$time$secret_key"
mac=$(echo -n $mac_raw | md5sum | awk '{print $1}')
header1="API-KEY:"$api_key
header2="API-REQUEST-DATE:"$time
header3="API-HMAC:"$mac
header4="API-FORMAT:json"
result=$(curl -k -X POST -H $header1 -H "$header2" -H $header3 -H $header4 -d "$data" $url)
if [[ $(echo $result | grep "success") != "" ]] ;then
logger -t NOTICE "CloudXNS DDNS success IP address $value"
fi
使用方法
- tomato:在当WAN联机时事件中添加本脚本。
- OpenWRT:
- 方法1:安装ddns-scripts和luci-app-ddns,在DDNS服务下添加本脚本。
- 方法2:将99-ddns放至/etc/hotplug.d/iface下,网卡启动时将自动运行本脚本,注意修改脚本中网卡名和你放置脚本的路径。