#!/bin/bash
echo "[$(date "+%G/%m/%d %H:%M:%S")] DDNSPOD.sh start..."
# 设置需要 DDNSPOD 的 APP_ID 和 API_Token
API_ID=xxxxxx
API_Token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 设置需要 DDNS 的一级域名 ,格式为 xxx.com
domain=xxx.com
# 设置需要 DDNS 的二级域名 ,格式为 aaa
host=aaa
# 设置获取本机 IP 需要测试的网站地址
CHECKURL="http://ip.03k.org"
. /etc/profile
date
if (echo $CHECKURL | grep -q "://"); then
IPREX='([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
URLIP=$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s $CHECKURL | grep -Eo "$IPREX" | tail -n1)
if (echo $URLIP | grep -qEvo "$IPREX"); then
URLIP="Get $DOMAIN URLIP Failed."
fi
echo "[URL IP]:$URLIP"
dnscmd="nslookup"
type nslookup >/dev/null 2>&1 || dnscmd="ping -c1"
DNSTEST=$($dnscmd $host.$domain)
if [ "$?" != 0 ] && [ "$dnscmd" == "nslookup" ] || (echo $DNSTEST | grep -qEvo "$IPREX"); then
DNSIP="Get $host.$domain DNS Failed."
else
DNSIP=$(echo $DNSTEST | grep -Eo "$IPREX" | tail -n1)
fi
echo "[DNS IP]:$DNSIP"
if [ "$DNSIP" == "$URLIP" ]; then
echo "IP SAME IN DNS,SKIP UPDATE."
exit
fi
fi
token="login_token=${API_ID},${API_Token}&format=json&lang=en&error_on_empty=yes&domain=${domain}&sub_domain=${host}"
Record="$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s -X POST https://dnsapi.cn/Record.List -d "${token}")"
iferr="$(echo ${Record#*code} | cut -d'"' -f3)"
if [ "$iferr" == "1" ]; then
record_ip=$(echo ${Record#*value} | cut -d'"' -f3)
echo "[API IP]:$record_ip"
if [ "$record_ip" == "$URLIP" ]; then
echo "IP SAME IN API,SKIP UPDATE."
exit
fi
record_id=$(echo ${Record#*\"records\"\:\[\{\"id\"} | cut -d'"' -f2)
record_line_id=$(echo ${Record#*line_id} | cut -d'"' -f3)
echo Start DDNS update...
ddns="$(curl -4 -k $(if [ -n "$OUT" ]; then echo "--interface $OUT"; fi) -s -X POST https://dnsapi.cn/Record.Ddns -d "${token}&record_id=${record_id}&record_line_id=${record_line_id}")"
ddns_result="$(echo ${ddns#*message\"} | cut -d'"' -f2)"
echo -n "DDNS upadte result:$ddns_result "
echo $ddns | grep -Eo "$IPREX" | tail -n1
else
echo -n Get $host.$domain error :
echo $(echo ${Record#*message\"}) | cut -d'"' -f2
fi
版权归属:
惬意小蜗牛
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区