Problems when parsing routing table and prefixes change

Hi,

I presume the following will need to turn in to a support request but I’m hoping someone has a work around. We walk the routing table looking for prefixes that match certain community strings and then add them to an array, to later add them to firewall address lists.

:foreach i in=[ /ip route find where active and bgp-communities ~ $community ] do={
  :set destination [ /ip route get $i dst-address ];
  :set prefixes [ $arrayXpush $prefixes $destination ];
}

Reference: http://forum.mikrotik.com/t/automating-address-list-maintenance-manrs-compliance/115375/1

The problem is that this can take almost 15 minutes. If we have any prefix churn during this time the loop starts returning inconsistent prefixes which don’t match the search criteria. I presume one work around would be to again validate that the prefixes contain the community value but I would expect the array to remain consistent.


Regards
David Herselman

Find returns array of route ids that were matched by criteria (this can take some time if table is large)
Foreach then goes through returned array which also takes time if array is very large.

And if routing table changed after find returned the result you may get unexpected results. If route was removed you may even get no such item error and script will stop (you can catch this error with try catch), but to validate if route still has the community you would need to make another check before adding it to an array or address list.