Community discussions

MikroTik App
 
hendranata
just joined
Topic Author
Posts: 13
Joined: Mon May 03, 2021 5:16 am

remove address list where prefix belongs to certain ip range

Tue Nov 16, 2021 4:21 am

helo
assume i have prefix:
2.2.2.0/24
3.3.3.0/24

how do we remove address beyond both above prefix?
sample:
address list 5.5.5.5 <--- this should be removed
address list 2.2.2.10 <-- this should be okay
address list 3.3.3.200 <-- this should be okay
address list 8.8.8.8 <-- this sould be removed.

any idea the script please?
thanks
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 725
Joined: Tue Dec 17, 2019 1:08 pm

Re: remove address list where prefix belongs to certain ip range

Wed Nov 17, 2021 4:50 am

#---------------------------------------
# Set IP-Addresses to Remove from Address-List
:local A2R 2.2.2.0/24
#---------------------------------------
/ip firewall address-list remove [find where address in $A2R]
 
hendranata
just joined
Topic Author
Posts: 13
Joined: Mon May 03, 2021 5:16 am

Re: remove address list where prefix belongs to certain ip range

Wed Nov 17, 2021 4:56 am

#---------------------------------------
# Set IP-Addresses to Remove from Address-List
:local A2R 2.2.2.0/24
#---------------------------------------
/ip firewall address-list remove [find where address in $A2R]
okay
thanks..

also how do we find where status enable only?
Last edited by hendranata on Wed Nov 17, 2021 5:29 am, edited 1 time in total.
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 725
Joined: Tue Dec 17, 2019 1:08 pm

Re: remove address list where prefix belongs to certain ip range

Wed Nov 17, 2021 5:26 am

Oh Man....
I should read Post more carefully...
Here is what you wanted

#---------------------------------------
# Set IP-Addresses to KEEP from Address-List
:local A2R {2.2.2.0/24;8.8.8.0/24;1.1.1.0/24} 
#---------------------------------------
:local KeepIP
:foreach i in=[/ip firewall address-list find] do={
	:set $KeepIP ""
	:foreach j in=$A2R do={
		:if ([/ip firewall address-list get $i address] in $j) do={
			:set $KeepIP true
		}
	}
	:if (!($KeepIP = true)) do={/ip firewall address-list remove $i}
}
 
ConnyMercier
Forum Veteran
Forum Veteran
Posts: 725
Joined: Tue Dec 17, 2019 1:08 pm

Re: remove address list where prefix belongs to certain ip range  [SOLVED]

Wed Nov 17, 2021 5:40 am

Added the Feature (delete only if enabled) for both Scripts.

Script 1:
#---------------------------------------
# Set IP-Addresses to Remove from Address-List
:local A2R 4.4.4.0/24
#---------------------------------------
/ip firewall address-list remove [find where address in $A2R && disabled=no]

Script 2:
#---------------------------------------
# Set IP-Addresses to KEEP from Address-List
:local A2R {2.2.2.0/24;8.8.8.0/24;1.1.1.0/24} 
#---------------------------------------

:local KeepIP
:foreach i in=[/ip firewall address-list find where disabled=no] do={
	:set $KeepIP ""
	:foreach j in=$A2R do={
		:if ([/ip firewall address-list get $i address] in $j) do={
			:set $KeepIP true
		}
	}
	:if (!($KeepIP = true)) do={/ip firewall address-list remove $i}
}

Who is online

Users browsing this forum: No registered users and 8 guests