Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 15394

Scripting • Re: delete address list old than 7 days

$
0
0
RouterOS 7.10.+ use Date on ISO format,
so something like "/ip firewall address-list remove [find where (creation-time<$daysago and list=$theList)]" work without use any clycle and is faster.
Just calc $daysago 30 days less than "today"

pseudocode, if RouterOS have such functions...
$daysago = [:todate [:toepoch [([:timestamp] - 2592000)]] ] ]
or better....
$daysago = ([:now] - 30d)
But since I wrote probably more scripting functions than RouterOS "programmers"...

viewtopic.php?p=977174#p977174

example for v7.10+ code

:global timetoseconds do={    :local inTime $1    :local wPos   [:find $inTime "w" -1]    :local dPos   [:find $inTime "d" -1]    :local itLen  [:find $inTime "." -1] ; :if ([:typeof $itLen] = "nil") do={:set itLen [:len $inTime]}    :local itSec  [:pick $inTime ($itLen - 2) $itLen]    :local itMin  [:pick $inTime ($itLen - 5) ($itLen - 3)]    :local itHou  [:pick $inTime ($itLen - 8) ($itLen - 6)]    :local itDay  0    :local itWee  0    :if (([:typeof $wPos] = "nil") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime 0 $dPos] }    :if (([:typeof $wPos] = "num") and ([:typeof $dPos] = "num")) do={:set itDay [:pick $inTime ($wPos + 1) $dPos] }    :if  ([:typeof $wPos] = "num")                                do={:set itWee [:pick $inTime 0 $wPos] }    :local totitSec ($itSec + (60 * $itMin) + (3600 * $itHou) + (86400 * $itDay) + (604800 * $itWee))    :return $totitSec}:global unixtodatetime do={    :local ux [:tonum $1]    :local Fzerofill do={:return [:pick (100 + $1) 1 3]}    :local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]    :local vgmt      [:tonum [/system clock get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}    :local tzepoch   ($ux + $vgmt)    :if ($tzepoch < 0) do={:set tzepoch 0} ; # unsupported negative unix epoch    :local yearStart (1970 + ($tzepoch / 31536000))    :local tmpbissex (($yearStart - 1968) / 4) ; :if ((($yearStart - 1968) % 4) = 0) do={:set ($prMntDays->1) -1 ; :set ($prMntDays->2) 30}    :local tmpsec    ($tzepoch % 31536000)    :local tmpdays   (($tmpsec / 86400) - $tmpbissex)    :if (($tmpsec < (86400 * $tmpbissex)) and ((($yearStart - 1968) % 4) = 0)) do={        :set tmpbissex ($tmpbissex - 1) ; :set ($prMntDays->1) 0 ; :set ($prMntDays->2) 31 ; :set tmpdays  ($tmpdays + 1)    }    :if ($tmpsec < (86400 * $tmpbissex)) do={:set yearStart ($yearStart - 1) ; :set tmpdays   ($tmpdays + 365)}    :local mnthStart 12 ; :while (($prMntDays->$mnthStart) > $tmpdays) do={:set mnthStart ($mnthStart - 1)}    :local dayStart  [$Fzerofill (($tmpdays + 1) - ($prMntDays->$mnthStart))]    :local timeStart (00:00:00 + [:totime ($tmpsec % 86400)])    :return "$yearStart/$[$Fzerofill $mnthStart]/$[$Fzerofill $dayStart] $timeStart"}{:local theList "bannedList":local hmanyds 30:local daysago [$unixtodatetime [$timetoseconds ([:timestamp] - ($hmanyds * 86400))]]/ip firewall address-list remove [find where (creation-time<$daysago and list=$theList)]}
i have tested this & this will delete entire list for some reason. not just more than 30 days old. sadly

Statistics: Posted by MLR — Sat Mar 16, 2024 3:10 am



Viewing all articles
Browse latest Browse all 15394

Trending Articles