因为一些原因,我想把自己家的的网络和爸妈家的网络连在一起,这边记录一下最终的做法:
硬件条件上,两端都用了 RouterOS,并且都是电信的 PPPoE 拨号上网。
建立 GRE 隧道
公网地址 | 互联地址 | 本地路由 | |
---|---|---|---|
SiteA | 1.1.1.1 | 192.168.100.1/30 | 192.168.11.0/24 |
SiteB | 2.2.2.2 | 192.168.100.2/30 | 192.168.15.0/24 |
# Site A
/interface gre
add name=ruohanGre remote-address=2.2.2.2
/ip address
add address=192.168.11.1/24 comment="default configuration" interface=ether2-LAN-master network=192.168.11.0
add address=192.168.100.1/30 interface=ruohanGre network=192.168.100.0
/ip route
add distance=1 dst-address=192.168.15.0/24 gateway=192.168.100.2
# Site B
/interface gre
add name=ruohanGre remote-address=1.1.1.1
/ip address
add address=192.168.15.1/24 comment=defconf interface=ether2-master network=192.168.15.0
add address=192.168.100.2/30 interface=ruohanGre network=192.168.100.0
/ip route
add distance=1 dst-address=192.168.11.0/24 gateway=192.168.100.1
开启 DDNS,获取本地 IP
# Run on SiteA and SiteB Ros
/ip cloud set ddns-enabled=yes
/ip cloud print
image.png
设置 IP 动态更新
思路上是先建立一个根据 DDNS 名来自动更新 IP 的脚本,然后再建立一个 disable
的调度器,每十秒钟执行一次更新 IP 脚本,接着建立两个脚本用来 Disable 和 Enable 这个调度器,最后建立一个健康检查,检查互联地址
最后的效果是:
- netwatch 长期 ping 监控对端互联地址,一旦互联地址不通就调用 down-script: enable 调度器的脚本
- 调度器被启用,调度器定时执行更新 IP 脚本,直到更新成功
- 更新成功后互联地址恢复,netwatch 就执行 up-script:disable 调度器,避免误用开销
下面这个配置在两个 site 都需要进行
/system script
add comment="update gre remote-address dynamically" name=ddnsGreRemoteIp owner=admin policy=read,write source=":local remoteDomainName \"your.domain.name\"\
\n:local greIfaceName \"ruohanGre\"\
\n:local greRemoteIp [:resolve \$remoteDomainName]\
\n:local currentGreRemoteIp [/interface gre get \$greIfaceName remote-address]\
\n:if (\$currentGreRemoteIp != \$greRemoteIp) do={\
\n /interface gre set \$greIfaceName remote-address=\$greRemoteIp;\
\n :log info \"set \$greIfaceName to remote-address=\$greRemoteIp\";\
\n}"
add name=EnaSetGre owner=admin policy=read,write source="/system scheduler enable setGre"
add name=DisaSetGre owner=admin policy=read,write source="/system scheduler disable setGre"
/system scheduler
add disabled=yes interval=10s name=setGre on-event="system script run ddnsGreRemoteIp" policy=read,write start-time=startup
/tool netwatch
add down-script=EnaSetGre host=192.168.100.2 interval=5s up-script=DisaSetGre
网友评论