第一个 get_datetime 脚本是规范日期格式的写法:

:global DateTime
:local Date [/system clock get date]
:local Time [/system clock get time]
:local Month [:tostr ([:find [:toarray "jan,feb,mar,apr,may,jun,jul,ago,sep,oct,nov,dec"] [:pick $Date 0 3]]+1)]
#if MM
#:if ([:len $Month]<2) do={:set Month "0$Month"}
# Format YYYY-M-D H:MM:SS
#\E5\B9\B4 \E6\9C\88 \E6\97\A5
:set DateTime ([:pick $Date 7 11]."-".$Month."-".[:tonum [:pick $Date 4 6]]." ".[:tonum [:pick $Time 0 2]].[:pick $Time 2 8])

第二个 Check_WAN_IP 脚本是检测到路由器的公网地址变化时自动发送钉钉机器人消息:
(此脚本使用的是钉钉消息的自定义关键字模式,指定的关键字是[路由器]即[\E8\B7\AF\E7\94\B1\E5\99\A8]这几个字符串,可自行修改。)

:global currentIP;
:global DateTime;
:execute "get_datetime"
:local newIP [/ip address get [find interface="pppoe-out1"] address];
:if ($newIP != $currentIP) do={
#    :put "ip address $currentIP changed to $newIP";
    :local header "Content-Type:application/json";
    :local dingtalk "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
    :local data "{\"msgtype\":\"text\",\"text\": {\"content\":\"$DateTime \E5\AE\BD\E5\B8\A6\E5\85\AC\E7\BD\91 IP \E5\8F\98\E6\9B\B4\E4\B8\BA: $newIP, [\E8\B7\AF\E7\94\B1\E5\99\A8]\"}}";
    :log info [/tool/fetch http-method=post mode=https http-header-field="$header" http-data="$data" url="$dingtalk"];
    :log warning "公网地址由 $currentIP 变为 $newIP";
    :set currentIP $newIP;
};

说明:此脚本中宽带拨号的接口是 pppoe-out1,用法是打开 /ppp/Profiles 下拨号使用的 profile 条目,在 script 标签页下的 On Up 对话框中输入如下内容(延迟三秒执行):

delay 3s
:execute "Check_WAN_IP"

标签: routeros, 钉钉, 公网

添加新评论