RouterOS定时开放与关闭端口

0
20

使用 **`enable/disable` 方法**来**定时控制 TCP 和 UDP 的 62066 端口开放与关闭(input + forward)**的完整方案。


一、目标

  • 每天 15:00 开启 TCP/UDP 的 62066 端口访问
  • 每天 16:00 禁用 TCP/UDP 的 62066 端口访问
  • 涉及 inputforward 两条链

二、防火墙规则添加(初始为禁用状态)

/ip firewall filter add chain=input protocol=tcp dst-port=62066 action=drop comment="block 62066 input tcp" disabled=yes
/ip firewall filter add chain=input protocol=udp dst-port=62066 action=drop comment="block 62066 input udp" disabled=yes

/ip firewall filter add chain=forward protocol=tcp dst-port=62066 action=drop comment="block 62066 forward tcp" disabled=yes
/ip firewall filter add chain=forward protocol=udp dst-port=62066 action=drop comment="block 62066 forward udp" disabled=yes

设置为 disabled=yes,表示默认允许访问,待关闭时再启用规则拦截。

三、创建脚本

1. 开启端口(即:禁用屏蔽规则 = 允许访问)

/system script add name="open_62066" source="\
/ip firewall filter disable [find comment=\"block 62066 input tcp\"] ;\
/ip firewall filter disable [find comment=\"block 62066 input udp\"] ;\
/ip firewall filter disable [find comment=\"block 62066 forward tcp\"] ;\
/ip firewall filter disable [find comment=\"block 62066 forward udp\"]"

2. 关闭端口(即:启用屏蔽规则 = 拒绝访问)

/system script add name="close_62066" source="\
/ip firewall filter enable [find comment=\"block 62066 input tcp\"] ;\
/ip firewall filter enable [find comment=\"block 62066 input udp\"] ;\
/ip firewall filter enable [find comment=\"block 62066 forward tcp\"] ;\
/ip firewall filter enable [find comment=\"block 62066 forward udp\"]"

四、设置定时任务(Scheduler)

/system scheduler add name="open62066" start-time=15:00:00 interval=1d on-event="/system script run open_62066"
/system scheduler add name="close62066" start-time=16:00:00 interval=1d on-event="/system script run close_62066"

五、验证状态

你可以随时查看规则状态:

/ip firewall filter print where dst-port=62066

查看 D(disabled)列是否正确切换。


如果防火墙里设置了这条规则:

/ip firewall filter add chain=forward connection-nat-state=!dstnat in-interface=WAN action=drop comment="defconf: drop all from WAN not DSTNATed"

如果一个流量是从 WAN 接口进来,且不是目标 NAT(dstnat)后的连接,那就丢弃它。

也就是说:

  • 只允许那些通过 dst-nat 做了端口映射的流量通过 forward 链
  • 其他从公网进来的请求(哪怕你开放了端口)也不会进入内网设备
  • 如果没有添加 dst-nat 规则将外部访问映射到本机或内网设备的端口,即使端口开放,该流量仍会被这条规则拦截!

哪就还需要设置外网访问内网端口映射

/ip firewall nat add chain=dstnat in-interface=WAN protocol=tcp dst-port=62066 action=dst-nat to-addresses=192.168.88.100 to-ports=62066 comment="port forward to local server"

发布回复

请输入评论!
请输入你的名字