Routeros路由 IP 变动推送Bark 服务器

脚本特点

  • IP 没变:直接退出,不重复删除/添加 address-list
  • 首次运行:初始化 address-list,不推送
  • IP 变化:更新 address-list,并推送 Bark
  • 接口 down:记录日志,不推送
  • 没有 IP:记录日志,不推送

推送内容:公网 IP 已变化: 旧IP → 新IP

Scheduler 每 1 分钟执行一次

/system scheduler
add name=update-auto-wan-ip interval=00:01:00 on-event="/system script run update-auto-wan-ip"

脚本

:local iface "pppoe-out1"
:local listName "auto-wan-ip"
:local ttl "1h"
:local barkUrl "http://192.168.1.10:8080/你的Key"

:if ([/interface get $iface running]=false) do={
    :log warning ("update-auto-wan-ip: " . $iface . " not running")
    :return
}

:local ids [/ip address find interface=$iface disabled=no]

:if ([:len $ids] = 0) do={
    :log warning ("update-auto-wan-ip: " . $iface . " no address")
    :return
}

:local id [:pick $ids 0]
:local current [/ip address get $id address]
:local ip [:pick $current 0 [:find $current "/"]]

:if ($ip = "") do={
    :log warning ("update-auto-wan-ip: empty ip")
    :return
}

:local oldIp ""
:local oldIds [/ip firewall address-list find list=$listName]

:if ([:len $oldIds] > 0) do={
    :local oldId [:pick $oldIds 0]
    :set oldIp [/ip firewall address-list get $oldId address]
}

:if ($oldIp = $ip) do={
    :return
}

:if ([:len $oldIds] > 0) do={
    /ip firewall address-list remove $oldIds
}

/ip firewall address-list add list=$listName address=$ip timeout=$ttl comment="auto"

:if ($oldIp = "") do={
    :log info ("update-auto-wan-ip: initialized " . $ip)
    :return
}

:log warning ("update-auto-wan-ip: public IP changed " . $oldIp . " -> " . $ip)

/tool fetch url=$barkUrl \
    http-method=post \
    http-header-field="Content-Type: application/x-www-form-urlencoded" \
    http-data=("title=RouterOS&body=%E5%85%AC%E7%BD%91%20IP%20%E5%B7%B2%E5%8F%98%E5%8C%96%3A%20" . $oldIp . "%20%E2%86%92%20" . $ip . "&group=RouterOS") \
    keep-result=no