Exporting bgp-communities value - problem

Hi guys,

I’m working on a project that requires me to retrieve a bgp-communities value from a RouterOS device and commit it to a text file, however am having a problem with the bgp-communities value.

The problem I’m having appears to be due to the fact that the bgp-communities is output like so

a) one community on a route exported looks like this

add bgp-communities=1234:1234 comment="Test 1"

b) multiple communities on a route exported looks like this

add bgp-communities=1234:1234,2222:2222 comment="Test 2"

My script cycles through the specific routes it wants to grab and uses the following line to commit the bgp-communities value to a variable.

:set bgpcommunities [/ip route get $entry value=bgp-communities]

And then commits it to part of a text file like so

:set entrydata ("This is route number $rnumber with the following BGP communities $bgpcommunities");

-------
If there’s one or no communities on the route.. no problem, it’ll continue on to the next line correctly and I get an output like so.

Routes:

add bgp-communities="" comment "1"
add bgp-communities=1234:1234 comment="2"

Output:

This is route number 1 with the following BGP communities 
This is route number 2 with the following BGP communities 1234:1234

------
However if there’s more than one community on a route it bugs out and cycles through the whole list for as many times as there are communities:

Routes:

add bgp-communities="" comment "1"
add bgp-communities=1234:1234,2222:2222 comment="2"

Output:

This is route number 1 with the following BGP communities 
This is route number 2 with the following BGP communities 1234:1234
,
This is route number 1 with the following BGP communities 
This is route number 2 with the following BGP communities 2222:2222

and for 3
Routes:

add bgp-communities="" comment "1"
add bgp-communities=1234:1234,2222:2222,1111:1111 comment="2"

Output:

This is route number 1 with the following BGP communities 
This is route number 2 with the following BGP communities 1234:1234
,
This is route number 1 with the following BGP communities 
This is route number 2 with the following BGP communities 2222:2222
,
This is route number 1 with the following BGP communities 
This is route number 2 with the following BGP communities 1111:1111

------
Which doesn’t make any sense to me, because the loop is only going by the number of routes there are, not anything else.

:foreach entry in=[/ip route find] do={
[admin@MikroTik] > {:foreach i in=[/ip route find routing-mark="test"] do={:local comm [:tostr [/ip route get $i bgp-communities]]; :put "Entry $i has \"$comm\" ";}}       
Entry *A has "" 
Entry *B has "1234:1234" 
Entry *C has "1234:1234,2345:2345" 
[admin@MikroTik] >

bgp-communities is actually an array, so string concatenation is executed for each element of array, and then all results are concatenated together with “;” separator:

[admin@MikroTik] > {:local arr [:toarray "1,2,3"]; :put ("Hello, array ".$arr);}  
Hello, array 1;Hello, array 2;Hello, array 3
[admin@MikroTik] >

that’s why you need to convert an arrat to string first

Ahh, that explains it :slight_smile:

Will give this a try this afternoon. Thanks for the pointer!

Worked perfectly. Thanks again.