

UniFi 搭配 HE DNS 配置延迟最小的 DDNS
source link: https://rabbit52.com/2018/07/unifi-he-ddns/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

UniFi 搭配 HE DNS 配置延迟最小的 DDNS
因为并没有用到 Unifi 中提供的 ddns 服务商:

而且 he 又提供一个非常方便的更新 ddns 记录的方法,所以这里尝试一个能及时更新 ddns 但又不至于请求接口太频繁的方法。
首先编辑一个更新 dns 的脚本,脚本的具体逻辑是读取拨号接口的 IP 并缓存,当 IP 发生变化时上报新的记录,这样不至于触发 he 的滥用限制,但是有可以通过定时任务将 ddns 延迟控制到1分钟以内:
#!/bin/bash
# 确保自己的公网 IP 在这个接口上
INTERFACE="pppoe0"
PRE_IP=`cat /tmp/ddns.ip`
# 将 {hostname} {key} 改成自己的,参考 https://dns.he.net/docs.html
URLS=(
"https://{hostname}:{key}@dyn.dns.he.net/nic/update?hostname={hostname}"
)
IP_RAW=`ip addr show dev $INTERFACE 2>&1`
if [ $? -ne 0 ]; then
logger -t ddns "get pppoe0 ip failed: $IP_RAW"
exit
fi
IP=`echo -e "${IP_RAW}" | grep "inet\b" | awk '{print $2}' | cut -d/ -f1`
if [ "$IP" != "$PRE_IP" ]; then
logger -t ddns "ip change to $IP"
for url in "${URLS[@]}";
do
CURL_RES=`curl -sSf4 "${url}" 2>&1`
# good ip address
# nochg ip address
if [ "${CURL_RES:0:4}" = "good" ] || [ "${CURL_RES:0:5}" = "nochg" ]; then
logger -t ddns "update ddns success: $CURL_RES"
else
logger -t ddns "update ddns failed: $CURL_RES"
exit
fi
done
echo $IP > /tmp/ddns.ip
fi
添加可执行权限后将其上传到 usg 的 /config/scripts/ddns.sh
备用。
接下来自定义 USG 的配置文件 config.geteway.json
添加如下配置段,实现每分钟执行 ddns 脚本以及定时重新拨号:
{
"system": {
"task-scheduler": {
"task": {
"ddns": {
"executable": {
"path": "/config/scripts/ddns.sh"
},
"interval": "1m"
},
"redial": {
"crontab-spec": "0 5 * * *",
"executable": {
"path": "/usr/bin/killall -HUP pppd"
}
}
}
},
}
}
因为电信的宽带一般两天左右会自动重拨并获取新的 IP,所以这里设置每天临晨 5 点自动重拨不影响白天使用。
将配置文件上传到 Unifi 的相应站点目录后强制同步 USG 即可,参考 Unifi 安装设置分享,测试下来表现很稳定:

并且重启路由器后脚本没有丢失,不过官方论坛上有人提到 USG 的固件更新后脚本可能会丢失,届时需要再次上传 ddns.sh
脚本。
Recommend
-
71
DNS - @keepeye - PING 1.1.1.1 (1.1.1.1): 56 data bytes64 bytes from 1.1.1.1: icmp_seq=0 ttl=251 time=4.348 ms64 b
-
16
宽带症候群 - @ileeoyo - 家里电信光纤,问客服要了动态公网 ip。第一次弄问下哪家 ddns 比较好,要不要备案?免费吗还是收费
-
13
UniFi Switch Port Profiles November 24, 2020 By Rowell Leave a Comment Consistency in c...
-
10
UniFi – USG Pro – Configuring Remote Access VPN March 7, 2020 By Rowell 2 Comments
-
12
Jan 20, 2021 | 0 Comments
-
7
Dec 10, 2020 |
-
4
V2EX › git gitlab 多域名 ddns 配置 chinafengzhao · 5 小时 6 分钟前 · 216 次点击
-
21
最近着手改造家里的网络,买了一个D2550软路由,把家里的光猫由拨号改成了桥接,并使用D2550拨号和管理家里的网络。很久之前就向电信申请了公网IP,一直没用上,此次趁着网络改造,顺带给这个公网IP配置DDNS,利用起来并为后续NAS的搭建提供外网访问的环境。
-
7
用户名需要使用你的登录邮箱地址 一开始我使用“区域 ID”作为用户名结果报错(code:6003) 运行日志如下 105857 : ddns version : 2.7.8-3 105857 : uci configuration: ddns.cf.domain='***Domain***'
-
3
V2EX › OpenWrt openwrt 动态 ddns 怎么配置 ssl 证书
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK