在使用动态ip的情况下使用tunnelbroker

最近我家不知为啥,pppoe拨号基本上每天晚上都会断一次,而重连之后由于ip地址变更,6to4的tunnel就中断了,一次两次就算了,每天都一次的话就不能靠手动,等靠自动化来维护。

参考:

https://wiki.mikrotik.com/wiki/Manual:Scripting-examples

https://gist.github.com/pklaus/960672

最开始是打算看看RouterOS能不能在接口启动之后触发运行一次脚本,但是ppp的profile里面的那个up down的scripts触发并没有效果(或许是我用法不对?)于是只能换一种思路,定时检查ip有没有变更,如果变更了,就触发一次配置更新。

两个脚本大概是这样:

脚本check_ip_change:检查ip有没有出现变更:

:global currentIP;
:local newIP [/ip address get [find interface="wan"] address];
:if ($newIP != $currentIP) do={
 /system script run update_sit;
 :set currentIP $newIP;
}

脚本update_sit:执行更新interface属性以及向tunnelbroker通知客户端节点地址变更:

local ipaddr
local users "someuser"
local key "somekey"
local tunnelid "sometunnelid"
:set ipaddr [/ip address get [/ip address find interface=wan] address]
:set ipaddr [:pick $ipaddr 0 ([len $ipaddr] -3)]
/interface 6to4 set sit1 local-address=$ipaddr
/tool fetch url="https://$users:[email protected]/nic/update?hostname=$tunnelid&myip=$ipaddr" mode=https keep-result=no

然后定制每分钟执行一次check_ip_change,然后在下次ip地址变更之后,就可以在1分钟内完成自动修复了。

发表评论