家用NAS利用公网IPV6外网访问
2019/10/20 计算机 次 0 条
通过IPV6从外网访问家里的NAS需要做两件事:一是解决IPV6的DDNS,二是配置路由器的防火墙。
先说DDNS,我用的是群晖的NAS,动态域名用的是Dnspod,但是群晖NAS自带的DDNS不支持Dnspod的IPV6更新。
只能自己写脚本来解决这个问题,直接上代码: 【以下代码在群晖DSM6系统下测试正常,其他设备可能需要做修改】
#!/bin/bash #Dnspod DDNS6 with BashShell #改编自Github:https://github.com/kkkgo/dnspod-ddns-with-bashshell #CONF START #API_ID和API_Token在dnspod的控制台 -> 用户中心 -> 安全设置 -> API Token 中获取 API_ID="12345" API_Token="abcdef123456abcdef123456abcdef123456" domain="yourdomain.com" host="yourhost" DEV="eth0" #CONF END date IPREX="((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))" DEVIP=$(ip -6 addr list scope global $DEV | grep -v " fd" | grep inet6 | awk '{print $2}' | head -n 1) if (echo $DEVIP | grep -qEvo "$IPREX");then echo "Get $DEV inet6 IP Failed." exit else DEVIP=$(echo $DEVIP | grep -Eo "$IPREX" | head -n 1) fi echo "[DEV IP]:$DEVIP" DNSTEST=$(nslookup -q=AAAA $host.$domain) if (echo $DNSTEST | grep -qEvo "$IPREX");then echo "Get $host.$domain DNS Failed." exit else DNSIP=$(echo $DNSTEST | grep -Eo "$IPREX" | tail -n 1) fi echo "[DNS IP]:$DNSIP" if [ "$DNSIP" == "$DEVIP" ];then echo "IP SAME IN DNS,SKIP UPDATE." exit fi token="login_token=${API_ID},${API_Token}&format=json&domain=${domain}&sub_domain=${host}" Record=$(curl -s -k -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" == "$DEVIP" ];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 -s -k -X POST https://dnsapi.cn/Record.Modify -d "${token}&record_id=${record_id}&record_line_id=${record_line_id}&value=${DEVIP}&record_type=AAAA") ddns_result="$(echo ${ddns#*message\"} | cut -d'"' -f2)" echo -n "DDNS upadte result:$ddns_result " echo $ddns | grep -Eo "$IPREX" | tail -n 1 else echo -n Get $host.$domain error : echo $(echo ${Record#*message\"}) | cut -d'"' -f2 fi
将以上代码保存为.sh文件,然后在NAS系统里添加一个计划任务: