RouterOS自动过滤24小时内已使用过IP地址
发布于2023-10-17 15:38 阅读1343次 RouterOS自动过滤24小时内已使用过IP地址,重复则自动断开再次ADSL拨号,客户端断开网络连接或关闭WIFI后,10秒延迟后自被主机自动监测到,就会断开相对应的ADSL连接,同时也就实现了客户端下线后自动换IP地址,需要IP供应商改为桥接模式才可实现,须拨号得到公网IP的环境下有效,内网10.x.x.x不生效。
以下为功能分段程序
```
#===========主机上线添加IP===============
/ip firewall address-list add list="onHost" address="192.168.1.1"
#===========主机下线删除IP===============
:delay 10
/tool netwatch add host=192.168.1.17 timeout=2s interval=500s comment="ls" down-script="/ip firewall address-list remove [/ip firewall address-list find list=\"onHost\" address=\"192.168.1.17\"];/tool netwatch remove [/tool netwatch find host=\"192.168.1.17\" comment=\"ls\"]" up-script="/tool netwatch remove [/tool netwatch find host=\"192.168.1.17\" comment=\"ls\"]"
#===========清除已使用IP记录=============
:local cHour 24
:foreach i in=[/ip firewall address-list find list="24h"] do={
/ip firewall address-list set $i comment=(tonum([/ip firewall address-list get $i comment]) + 1)
:if (tonum([/ip firewall address-list get $i comment]) > $cHour) do={
/ip firewall address-list remove $i
}
}
#===========临时手动释放IP===============
:foreach i in=[/ip firewall address-list find list="24h"] do={
/ip firewall address-list remove $i
}
#===========自动拨号程序=================
:local curInt
:local curAdd
:for i from=1 to=55 do={
:local curInt ("pppoe-out" . $i)
:local curAdd ""
:local stcAdd "192.168.1."
:if ([/ip firewall address-list find list=onHost address=($stcAdd . $i)]!="") do={
#上线的用户分配线路
:if ([/interface pppoe-client get [/interface pppoe-client find name=$curInt] running ]=false) do={
/interface pppoe-client enable [/interface pppoe-client find name=$curInt]
:delay 8
#上线用户获取接口IP地址,如果已经用过就禁用,重拨;没有用过就使用并做24小时记录
:if ([/ip address find interface=$curInt]!="") do={
:set curAdd [:pick [/ip address get [/ip address find interface=$curInt] address ] 0 end=[:find [/ip address get [/ip address find interface=$curInt] address ] "/"]]
:log error ($curInt . " 拨号成功 IP:" . $curAdd)
} else {
/interface pppoe-client disable [/interface pppoe-client find name=$curInt]
}
:if ($curAdd!="") do={
#已经获取公网IP的对比做决定
:if ([/ip firewall address-list find list="24h" address=$curAdd]="") do={
/ip firewall address-list add list="24h" address=$curAdd comment="1"
/ip firewall nat enable [/ip firewall nat find src-address=($stcAdd . $i)]
:log war ($curInt . " 获得新IP:" . $curAdd)
} else={
/interface pppoe-client disable [/interface pppoe-client find name=$curInt]
:log error ($curInt . " IP地址重复,禁用重拨 " . $curAdd)
}
}
}
} else={
#下线的用户禁用线路
#:log error $curInt
:if ([/interface pppoe-client get [/interface pppoe-client find name=$curInt] disabled]=no) do={
/interface pppoe-client disable [/interface pppoe-client find name=$curInt]
/ip firewall nat disable [/ip firewall nat find src-address=($stcAdd . $i)]
:log error ($curInt . "终端下线,接口已禁用")
}
}
}
```