Hi guys,
Back with another script-related query today ![]()
Trying to write a section of code that will allow me to take a variable and allow it to be inserted (or not inserted) as part of an ‘add’ command on a RouterOS device.
Example:
/ip route add dst-address=192.168.1.0/24 gateway=192.168.0.254
vs
/ip route add dst-address=192.168.1.0/24 gateway=192.168.0.254 bgp-local-pref=1111
I want to be able to run this command from inside a script, which in itself is easy enough, however the “src-address” value (or other values) should only be included if they exist.
The way I’m trying to do this currently works like so, I read/define each value throughout the script then when I get to the add command I use all of these values at once like so
/ip route add dst-address=$dstaddress gateway=$gateway bgp-local-pref=$bgplocalpref
which then gives me an error as although I can enter a blank value for bgp-local-pref in a terminal window, having it do so from a script causes an error.
admin@MikroTik] > ip route add dst-address=192.168.1.0/24 gateway=192.168.0.254 bgp-local-pref=
[admin@MikroTik] > system script run test
invalid value for bgp-local-pref, an integer required
[admin@MikroTik] >
As you can see, the terminal accepts no value fine.. while the test script (listed below) causes an error.
:local dstaddress "192.168.1.0/24"
:local gateway "192.168.0.254"
:local bgplocalpref
/ip route add dst-address=$dstaddress gateway=$gateway bgp-local-pref=$bgplocalpref
So, I figure.. why not just check if the value exists at all and if not don’t call it as part of the command.
/ip route add dst-address=$dstaddress gateway=$gateway [:if ([:len $bgplocalpref] > 0 ) do={bgp-local-pref=$bgplocalpref}]
However this doesn’t work either. Now it would be easy enough to just write 2 /ip route add statements based on bgp-local-pref however I’m actually trying to work with a number of the bgp values so for each value I’d be required to write a number more /ip route add statements (for 5 attributes, 32 statements are required).
So.. I went for an alternative solution: set the values to be part of the command as well.
:if ([:len $bgplocalpref] > 0 ) do={:set bgplocalpref "bgp-local-pref=$bgplocalpref"}
/ip route add dst-address=$dstaddress gateway=$gateway $bgplocalpref
However this also simply generates an error (I’d expected that it’d just continue on with the ‘add’ command if there was no value assigned to the variable.
Out of ideas now.. anyone else run into this before?