Hello, how can I access to read the timeout value of an entry in the address list of the firewall within a script?
Greetings and thank you very much.
:foreach list in=[/ip/firewall/address-list/find where list=<LIST_NAME>] do={
:local timeout [get value-name=timeout $list]
}
/ip firewall address-list
:foreach item in=[find where ([:typeof $timeout]!="nil")] do={
:put [get $item]
}
:foreach list item in=[/ip/ firewall/ address-list/ find where list="<LIST_NAME>"] do={
:local timeout tout [get value-name=timeout $list $item timeout]
}
/ip firewall address-list
{
:local listName "test"
:local itemTimeout 0
:foreach item in=[find where list=$listName] do={
:set itemTimeout [get $item timeout]
:if ([:typeof $itemTimeout]="nil") do={:set itemTimeout 0}
:put "Timeout is $itemTimeout for IP $[get $item address] on address list $listName"
}
}
I don't own device pre ROS 7 version, so I cannot check syntax on other versions, thats why specified it because it's checked. Since OP author did not specify on which ROS version needs code this may or not may be useful to him, if is not, I guess he will reply that is not working and someone else will reply like you for eg.@optio if it's not something that's only on 7, like for example wireguard, better provide a syntax that works on any version...
I overlooked that, yes same var. name as property should be avoided.Why not write no-frills code? Perhaps with variables consistent with what they contain, and without using names that are also words that could create problems?
Why add unnecessary condition check if timeout exists, maybe OP wants to check timeout on item in list that always contains timeout? It was not specified, from question in OP I guessed it is that case. If you want, you can add more conditions, like add condition to get timeout for certain ip/host in address list if there are more in it, etc... It easy to add "improvements" to code when you don't know exact requirement.Code: Select all/ip firewall address-list { :local listName "test" :local itemTimeout 0 :foreach item in=[find where list=$listName] do={ :set itemTimeout [get $item timeout] :if ([:typeof $itemTimeout]="nil") do={:set itemTimeout 0} :put "Timeout is $itemTimeout for IP $[get $item address] on address list $listName" } }
/ip/firewall/address-list> /ip/firewall/address-list/; :foreach item in=[find list=<LISTNAME> timeout] do={:put [get $item]}
/ip/firewall/address-list> /ip/firewall/address-list/; :foreach item in=[find list=<LISTNAME> !timeout] do={:put [get $item]}
/ip firewall address-list
add list=test timeout=2m address=6.6.6.6 disabled=no comment="test"
add list=test address=7.7.7.7 disabled=no comment="test"
:foreach item in=[find list=test timeout] do={:put [get $item]}
:foreach item in=[find list=test !timeout] do={:put [get $item]}
[@] /ip firewall address-list> /ip firewall address-list [@] /ip firewall address-list> add list=test timeout=2m address=6.6.6.6 disabled=no comment="test" [@] /ip firewall address-list> add list=test address=7.7.7.7 disabled=no comment="test" [@] /ip firewall address-list> :foreach item in=[find list=test timeout] do={:put [get $item]} [@] /ip firewall address-list> :foreach item in=[find list=test !timeout] do={:put [get $item]} .id=*dee1;address=7.7.7.7;comment=test;creation-time=jul/10/2023 17:14:35;disabled=false;dynamic=false;list=test [@] /ip firewall address-list>
[@] /ip/firewall/address-list> /ip firewall address-list [@] /ip/firewall/address-list> add list=test timeout=2m address=6.6.6.6 disabled=no comment="test" [@] /ip/firewall/address-list> add list=test address=7.7.7.7 disabled=no comment="test" [@] /ip/firewall/address-list> :foreach item in=[find list=test timeout] do={:put [get $item]} .id=*1005;address=6.6.6.6;comment=test;creation-time=2023-07-10 17:17:15;disabled=false;dynamic=true;list=test;timeout=00:01:52 [@] /ip/firewall/address-list> :foreach item in=[find list=test !timeout] do={:put [get $item]} .id=*1006;address=7.7.7.7;comment=test;creation-time=2023-07-10 17:17:15;disabled=false;dynamic=false;list=test
/ip firewall address-list ; :foreach item in=[find list=test [:typeof $timeout]!="nil"] do={:put [get $item]}
:foreach item in=[find list=<LIST NAME> timeout~"."] do={:put [get $item]}
:foreach item in=[find list=<LIST NAME> !timeout] do={:put [get $item]}
Flags: X - disabled, D - dynamic
# LIST ADDRESS CREATION-TIME TIMEOUT
0 ;;; test
test 7.7.7.7 jul/10/2023 19:46:53
1 D test 1.1.1.1 jul/10/2023 19:57:09 1h39m51s
2 D test 192.168.255.255 jul/10/2023 20:07:14
@ /ip firewall address-list> :foreach item in=[find list=test timeout~"."] do={:put [get $item]}
.id=*6;address=1.1.1.1;creation-time=jul/10/2023 19:57:09;disabled=false;dynamic=true;list=test;timeout=01:43:30
@ /ip firewall address-list> :foreach item in=[find list=test !dynamic] do={:put [get $item]}
.id=*2;address=7.7.7.7;comment=test;creation-time=jul/10/2023 19:46:53;disabled=false;dynamic=false;list=test
@ /ip firewall address-list> :foreach item in=[find list=test dynamic !timeout] do={:put [get $item]}
.id=*c;address=192.168.255.255;creation-time=jul/10/2023 20:07:14;disabled=false;dynamic=true;list=test
Nice.... effectively, it's more elegant than mine.timeout~"."