Simple script for set pref-source

Hi,

I’m trying to set pref-source on specific route on multiple device with the LAN address on each one router but I have problem. When I’m trying with this script nothing it happen.
For example I have:

/ip address
add address=10.0.5.1/24 comment=“default configuration” interface=LAN network=10.0.5.0
/ip route
add distance=1 dst-address=10.120.0.0/16 gateway=10.10.10.1

:local langw1 [/ip address get [find interface=“LAN”] address];
:local perfsrc1 [/ip route get [find dst-address=10.120.0.0/16 pref-src];

:if ($langw1 != $perfsrc1) do={
/ip route set pref-src=$langw1 [find dst-address=10.120.0.0/16];
}

I want to set 10.0.5.1 for “pref-src” but as variable value because on each one router this value will be different.

Can you help me with this?

Thank you!

when you get the address, you get also the subnet mask /xx
pref-src do not have subnet mask and you can not use ip with subnet mask on set
you can not compare two values like (“127.0.0.1” = “127.0.0.1/24”)

remove the subnet mask with :pick before use the address as pref-src and before use it on compare

Thank you!

I changed it with this, because for first time I use scripts and don’t know how to use :pick yet, but I still have problem:

:local langw1 [/ip dhcp-server network get value-name=gateway number=0];
:local perfsrc1 [/ip route get [find dst-address=10.120.0.0/16] pref-src];

:if ($langw1 != $perfsrc1) do={
/ip route set pref-src=$langw1 [find dst-address=10.120.0.0/16];
}

or

:local langw1 [/ip dhcp-server network get value-name=gateway number=0];
:local perfsrc1 [/ip route get [find dst-address=10.120.0.0/16] pref-src];

:if ($perfsrc1 = 0.0.0.0) do={
/ip route set [find dst-address=10.120.0.0/16] pref-src=$langw1;
}

on all routers I have only one dhcp server for LAN network (for example gateway is set 10.0.5.1)