利用「Server酱」推送路由器DDNS更新消息
lyq1996

意义

路由器的公网IP一旦变动,自动更新cloudflare ddns,同时微信推送一条消息,效果图如下。
image

「Server酱」

超好用的工具,不仅支持从路由器推送到微信,还支持从微信发语音到server酱触发webhook。

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/sh
export PATH='/usr/sbin:/usr/bin:/sbin:/bin'

# Paraments
resub=1
push1="1" # Push online devices
push_ddns="1" # Push ddns message
serverchan_enable="1" # Enable ServerChan
serverchan_sckey="" # ServerChan api key
APITOKEN="" # Your API Token
ZONEID="" # Your zone id, hex16 string
RECORDNAME="" # Your DNS record name, e.g. sub.example.com
RECORDTTL="1" # TTL in seconds (1=auto)

touch /tmp/home/root/lastIPAddress
[ ! -s /tmp/home/root/lastIPAddress ] && echo "爷刚启动!" > /tmp/home/root/lastIPAddress

touch /tmp/home/root/pushDevice
[ ! -s /tmp/home/root/pushDevice ] && echo "$push1" > /tmp/home/root/pushDevice

# Get wan IP
# Check curl exist, if not, use wget
getIpAddress() {
curltest=`which curl`
if [ -z "$curltest" ] || [ ! -s "`which curl`" ] ; then
wget --no-check-certificate --quiet --output-document=- "http://members.3322.org/dyndns/getip"
else
curl -k -s "http://members.3322.org/dyndns/getip"
fi
}
# load last IP
lastIPAddress() {
local inter="/tmp/home/root/lastIPAddress"
cat $inter
}

ddnsUpdate() {
IP=${1}
# Fetch DNS record ID
RESPONSE="$(
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${ZONEID}/dns_records?page=1&#per_page=1000&order=type&direction=asc" \
-H "Authorization: Bearer ${APITOKEN}" \
-H "Content-Type:application/json"
)"
RECORDID="$(echo ${RESPONSE} | sed -n "s/.*\"id\":\"\([^\"]*\)\".*\"name\":\"${RECORDNAME}\".*/\1/p")"

# Update DNS record
RESPONSE="$(
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${ZONEID}/dns_records/${RECORDID}" \
-H "Authorization: Bearer ${APITOKEN}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"${RECORDNAME}\",\"content\":\"${IP}\",\"ttl\":${RECORDTTL},\"proxied\":false}"
)"

# Check whether the update was successful
echo $RESPONSE | grep '"success":\ *true' >/dev/null

if [ $? -eq 0 ]; then
logger -t "ddns" "Cloudflare ddns updated"
return 0
else
logger -t "ddns" "Cloudflare ddns updated failed"
return 1
fi
}

# Get online devices
test(){
alias=`cat /var/lib/misc/dnsmasq.leases`
arp | sed 's/(//;s/)//' | while read -r DESC IP AT MAC ETH ON IFACE
do
if [ $IFACE ]; then
NAME=`echo "$alias" | awk '/'$MAC'\ '$IP'/{print $4}'`
echo $NAME $IP >> /tmp/home/root/newhostname.txt
fi
done
}

while [ "$serverchan_enable" = "1" ];
do
curltest=`which curl`
if [ -z "$curltest" ] ; then
wget --continue --no-check-certificate -q -T 10 http://www.baidu.com
[ "$?" == "0" ] && check=200 || check=404
else
check=`curl -k -s -w "%{http_code}" "http://www.baidu.com" -o /dev/null`
fi

if [ "$check" == "200" ] ; then
local hostIP=$(getIpAddress)
local lastIP=$(lastIPAddress)

if [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ] ; then
sleep 60
# Check again
local hostIP=$(getIpAddress)
local lastIP=$(lastIPAddress)
fi

if [ "$lastIP" != "$hostIP" ] && [ ! -z "$hostIP" ] ; then
logger -t "公网IP变动" "目前 IP: ${hostIP}"
logger -t "公网IP变动" "上次 IP: ${lastIP}"
ddnsUpdate ${hostIP}
if [ "$?" == "0" ] ; then
if [ "$push_ddns" = "1" ] ; then
curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=AC9的DDNS更新啦" -d "&desp=${hostIP}" &
logger -t "wechat push" "IP: ${hostIP} pushed"
fi
echo -n $hostIP > /tmp/home/root/lastIPAddress
fi
fi

if [ `cat /tmp/home/root/pushDevice` = "1" ] ; then
# 设备上、下线提醒
# 获取接入设备名称
touch /tmp/home/root/newhostname.txt
echo "接入设备名称" > /tmp/home/root/newhostname.txt
# 当前所有接入设备
test
# cat /tmp/syslog.log | grep 'Found new hostname' | awk '{print $7" "$8}' >> /tmp/home/root/newhostname.txt # MAC and IP
# cat /tmp/static_ip.inf | grep -v "^$" | awk -F "," '{ if ( $6 == 0 ) print "【内网IP:"$1", MAC:"$2", 名称:"$3"】"}' >> /tmp/home/root/newhostname.txt
# 读取已在线设备名称
touch /tmp/home/root/hostname_online.txt
[ ! -s /tmp/home/root/hostname_online.txt ] && echo "接入设备名称" > /tmp/home/root/hostname_online.txt
# 上线
# awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' /tmp/home/root/hostname_online.txt /tmp/home/root/newhostname.txt > /tmp/home/root/newhostname_uniqe_online.txt
awk 'NR==FNR{a[$0]++} NR>FNR&&a[$0]' /tmp/home/root/hostname_online.txt /tmp/home/root/newhostname.txt > /tmp/home/root/newhostname_same_online.txt
awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' /tmp/home/root/newhostname_same_online.txt /tmp/home/root/newhostname.txt > /tmp/home/root/newhostname_uniqe_online.txt
if [ -s "/tmp/home/root/newhostname_uniqe_online.txt" ] ; then
content=`cat /tmp/home/root/newhostname_uniqe_online.txt | grep -v "^$"`
curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=AC9有设备上线啦" -d "&desp=${content}" &
logger -t "wechat push" "设备上线: ${content} pushed"
cat /tmp/home/root/newhostname_uniqe_online.txt | grep -v "^$" >> /tmp/home/root/hostname_online.txt
fi
# 下线
awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' /tmp/home/root/newhostname.txt /tmp/home/root/hostname_online.txt > /tmp/home/root/newhostname_uniqe_offline.txt
if [ -s "/tmp/home/root/newhostname_uniqe_offline.txt" ] ; then
content=`cat /tmp/home/root/newhostname_uniqe_offline.txt | grep -v "^$"`
curl -s "http://sc.ftqq.com/$serverchan_sckey.send?text=AC9有设备下线啦" -d "&desp=${content}" &
logger -t "wechat push" "设备下线: ${content} pushed"
cat /tmp/home/root/newhostname.txt | grep -v "^$" > /tmp/home/root/hostname_online.txt
fi
fi
resub=`expr $resub + 1`
[ "$resub" -gt 360 ] && resub=1
else
logger -t "server chan" "Check network failed."
resub=1
fi
sleep 60
continue
done


自启动

随机应变

 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
访客数 访问量