随机推荐

RouterOS + Bark 网络状态推送实战

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

Routeros外网恢复推送Bark 服务器

外网恢复提醒:每 3 分钟检查一次状态。

一直 ping 通:不重复推送 从通变不通:执行 down-script 从不通变通:执行 up-script,推送一次“外网恢复” 之后继续通:不再推送

手动执行 up-script 里的 /tool fetch … 命令,那会立即收到通知;但 Netwatch 正常运行时,不会按 interval 重复推送。

/tool netwatch
add host=223.6.6.6 interval=00:00:30 timeout=3s 
down-script=":log warning "netwatch: 222.172.200.68 down"" 
up-script="/tool fetch url="http://192.168.1.10:8080/你的Key" http-method=post http-header-field="Content-Type: application/x-www-form-urlencoded" http-data="title=%E8%B7%AF%E7%94%B1%E5%99%A8%E9%80%9A%E7%9F%A5&body=%E5%A4%96%E7%BD%91%E6%81%A2%E5%A4%8D&group=RouterOS" keep-result=no"

Routeros设备离线推送Bark 服务器

标题:路由器通知 内容:局域网设备离线: 192.168.1.50

/tool netwatch add host=192.168.1.50 interval=00:03:00 timeout=3s down-script="/tool fetch url="http://192.168.1.10:8080/你的Key" http-method=post http-header-field="Content-Type: application/x-www-form-urlencoded" http-data="title=%E8%B7%AF%E7%94%B1%E5%99%A8%E9%80%9A%E7%9F%A5&body=%E5%B1%80%E5%9F%9F%E7%BD%91%E8%AE%BE%E5%A4%87%E7%A6%BB%E7%BA%BF%3A%20192.168.1.50&group=RouterOS" keep-result=no" up-script=":log info "LAN device 192.168.1.50 online""