Community discussions

 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 1302
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Help to simplify address log script

Fri Jun 14, 2019 3:22 pm

I have a script to get all address list with number of dynamic and static entries like this: (comments added to be able to understand it :) )

[
# Sett arryays to empty string to declare them

# This holds unique name of lists
:local array [ :toarray "" ]

# Used to count dynamic address list
:local addrcntdyn [:toarray ""] 

# Used to count static address list
:local addrcntstat [:toarray ""] 

# Loop trough all address inn address-lists
:foreach id in=[/ip firewall address-list find] do={

# Get the entry
	:local rec [/ip firewall address-list get $id]

# Get name of list
	:local listname ($rec->"list")
	
# Get if its dynamic or not	
	:local listdynamic ($rec->"dynamic")
	
# Store evry enuque liat name to "array"	
	:set ( $array->$listname ) 1

# Test if its dynamic or static	
	if ($listdynamic = true) do={
	
# If dynamic increment k,v pair by 1	
		:set ($addrcntdyn->$listname) ($addrcntdyn->$listname+1)
	} else={
	
# If static increment k,v pair by 1		
		:set ($addrcntstat->$listname) ($addrcntstat->$listname+1)}
}

# Run troug all lists found in "array"
:foreach k,v in=$array do={

# log list name and number of dynamic and static entry
#	:log info message=("script=address_lists list=$k dynamic=".(($addrcntdyn->$k)+0)." static=".(($addrcntstat->$k)+0))

# Just for test, print instead of log
	:put ("script=address_lists list=$k dynamic=".(($addrcntdyn->$k)+0)." static=".(($addrcntstat->$k)+0))
}

]
Typical output: (This is what I get and its how I would like the output to be)
script=address_lists list=Blocked dynamic=1 static=7
script=address_lists list=Clients dynamic=0 static=2
script=address_lists list=FW_Block_user_try_unkown_port dynamic=1079 static=0
script=address_lists list=IPSEC dynamic=1 static=0
script=address_lists list=Router dynamic=0 static=1
script=address_lists list=Whitelist_IP dynamic=3 static=2
script=address_lists list=black_list_ftp dynamic=1 static=0
script=address_lists list=black_list_rdp dynamic=41 static=0
script=address_lists list=rdp_stage1 dynamic=28 static=0
script=address_lists list=rdp_stage2 dynamic=24 static=0

You can just cut and past this script to your system for test,

But can it be made simpler? faster?
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 14 guests