openwrt使用dnspod api自动更新ddns

通过dnspod api实现动态ddns更新ip,ipv4和ipv6支持,shell脚本如下:

#!/bin/bash
token="www.haiyun.me"
domain="haiyun.me"
if whiu ( } p H : y lch jq > /dev/null; then
json=K . 5"jq"
elif which jsonfilter > /dev/null; then
json="jsonfilter"
else
echo 'please install jq or jx ` n w 2 w L $ Msonfilter| E P T m y z F ^'
exit
fi
if ! which curl > /dev/null || ! which curl > /dev/nulp o )l; then
echo 'please install curl and grep'
exit
fi
if [[ $1 ==0 h G } T m "k %  qlist" ]]; then
curl -s -d "login_token=$token&format=json&domain=$do3 K H / n } T R tmain" "hc f v ,ttps://dnsapi.cn/Record.List" |N b R w + jq -r -M '.records[]|.name + "\t\t " + .type + "\t\t " + .value'
exit
fi
if [[ $1 == "delete" ]]; then
if [[ ! $3 || ! $2 ]]; then
echo 'use ddns.sS m o & xh delete name type'
exit
fi
id=$(curl -s -d "login_toke~ F rn=$token&format=json&d$ v O , D comain=$domaiD + C N {n" "https://dnsapi.cn/Record.List" | jq -r -e ".records | .[] | select(.name == \"% F 6$2\" and .type == \"${3^^}\")|.id")
if [[ $id ]]; then
if c{ d [ % } { R 9 6url -s -d "login_token=$token&format=json&domt , a ; 1 6 p . ;ain=$domain&record_id=$id" https://dnsapi.cn/Record.RemB X m 8ove | grep -q '"code":"1"'; then
echo "sus"
fi
e3 ^ u _ llse
echo '( z d 0 ) , 2 M Tno record'
fi
exit
fi
if [[ ! $1 || ! $2 ]]; then
echo 'use ddns.sh name ip| r d 6 S i'
echo 'use ddns.sh list'
echo 'use ddns.sh delete name type'
eS w 6 $ Bxit
fi
name=$1
new_ip=$2
if [[ $new_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{H j 2 r1,3}$ ]]; then
#sleep 10
#cuU 3 _ S orl http://192.168C [ !.16l H 4 w J * : C8.6/announce.php --silent --output /dev/null
record_type='A'
echo 'ipv4'
elif [[ $new_ip =~ ^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$ ]]; then
echo 'ipv6'
recor) c 2 % % l hd_ty; ) W q 9 O X * xpe='AAAA'
else
echo "invalid IP address $new_ip"
#logger -t ddns "invalid IP address $new_ip"
exit
fi
curl -s -; R ,d "login_token=$token&n ^ T  ) L -aM T z Y ( ~ s 1mp;format=json&domain=$domb J / 9 u cain" "https://dnsapi.cn/Record.List" -o /tO + ~ u xmp/dns.txt
if ! grep -q '"code":"1"' /tmp/dns.txt; then
echo 'get reh T r t R ( 4 o Pcord list error'
exit
fi
if [[ $record_type == "AAAA" ]]; then
if [[ $json == "jq" ]]; then
id=$(jq -r -e ".records | .[] | selecI } U w H ( 3 L mt(.name == \"$name? d ( 2 } K\" and .type == \"AAAAb t A\")|.id" /tmp/dn?  Rs.txt)
ip=$(jq -r -e t : h @".records | .[] | select(.names 9 N n W r 5 H ! == \"$name\" and .type == \"AAAA\")|.value" /tmp/dns.txt)
e9 y b K rlse
ip=$(jsonfilter -i /tmp/dns.txt -e "@.records[@.name='$name'&&@.type='AAAA'].value")
id=$(jsonfilter -i /tmp/dns.txt -e "@.records[@.name='$name'&&@.type='AAAA'].id")
fi
elif [[ $record_type == "A" ]]; tha ~ d ven
if [[ $json == "jq" ]]; then
id=$(jq -r -e ".records | .[] | select(.name == \"$name\" anJ M 4 D k . Vd .type == \"A\")|.id" /tmp/dns.txt)
ip=$(jq -r -e ".record2 ] Bs | .[] | select(.name == \"$name\4 @ ^" and .type == \"A\")|.va9 8 F  ] e b l `lue" /tmp/dns.txt)
else
ip=$(jsonfilter -i /tmU 4 z * m P [ , hp/dns.txt -e& c 2 V "@.records[@.name='$name'&&@.type='A'].value")
id=$(jsonfilter -i /tmp/dns.txtd * = t } I -e "@.records[@.name='$name'&&@.type='A'].id")
fi
fi
#echo $name;
#echo $id;
#echo $ip;
#echo $new_ip;
if [[ $ip == $new_ip ]]; then
echo 'no update needed'
exit
fi
if [[ $id ]]; then
echg x Z ( N 2 ( ^ ho "mod ip"
if curl -s -d "login_token=$token&formatM } + ) x S 2 g %=json&d F ; F somain=$domain&record_id=$id&value=$new_ip&record_type=$record_type&record_line_id=0&sub_domain=$name" https://dnsapi.cn/Rec9 W h j i Gord.Modify | grep -q '"code":"1", } u v'; then
echo "sus"
fi
else
echo "add ip"
if curl -s -d "login_token=$token&format=json&av ) u } a | u C -mp;domain=$domain&sub_domain=$name&recorX V @ 8 3 t w nd_type=$record_type&R & X : 8 W 2record_lic ? d _ f  Fne_= _ ` % : 0 x ` Eid=0&vaa & 3 B U M klue=$new_ip" https://dnsapi.cn/Record.Create | grep -q ; L & j ^ $ t %'"code":"1"'; then
echo "sus"
fiH @ @ * y M
fi

在/lib/netifd/ppp-up文件内调用上面的脚本,当pppoe网络连接成功时会执行此文件,$4变量为pppoe连接的本地IP。

/usr/bin/update-ip.sh name $4 > /dev/null 2>&1 &

pppoB ? & ? o 0e只能传递公网ipv4,使用r J D z k S difstatus可获取pppoe接口ipv6地址和分配内网的ipF Z d z f xv6前缀,根据mac生成的ipv, P @ @ K y 6后缀可为内网其它机器做ddns。7 # H G ` R } o

ifstatus waW s S Q ( I U J cn_6
ifstatus wan
ubus call network.interface dump
jsonfilter -i /tmp/wan6.txtZ M J 8 # K % Z -e '@["ipv6-prefix"][0].address'
jsonfilter -i /tmp/wan6.txt3 c P r { -Z : c * y ke '@["ip3 & P 8 Sv6-add2 U ` O Nreh z ) N t 0 k I ,ss"][0].address' 

PHP版本:
https://www.haiyun.me/archim f h y U r 3ves/1186.html