ROS 建立在无固定 IP 上的 GRE 隧道

因为一些原因,我想把自己家的的网络和爸妈家的网络连在一起,这边记录一下最终的做法:

硬件条件上,两端都用了 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 这个调度器,最后建立一个健康检查,检查互联地址

最后的效果是:

  1. netwatch 长期 ping 监控对端互联地址,一旦互联地址不通就调用 down-script: enable 调度器的脚本
  2. 调度器被启用,调度器定时执行更新 IP 脚本,直到更新成功
  3. 更新成功后互联地址恢复,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

参考

  1. Ros Manual:Interface/Gre
  2. Using a dynamic dns name for a VPN server
  3. VPN (any type) between 2 Mikrotik routers and no static IP addresses
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,196评论 6 13
  • 坐了六个多小时的高铁 总算抵达目的地——淄博 但是随之而来的是我最不情愿的事 就是陪同国医还有领导吃饭 这顿饭虽然...
    羽衣甘蓝2025阅读 303评论 1 0
  • 问:什么事情最容易 答:吐槽 因为最近实在事务烦多,加上娃们精力旺盛,睡眠时间压缩到六个小时。昨夜只睡了四个小时。...
    alisa思思阅读 442评论 0 0
  • 企业内的成功,需要能够进入老板的视线范围,特别是年轻人或新入职的。要进入老板的视线范围,方法很多,但不外乎是工作态...
    荣来书院阅读 348评论 1 3