Hello forum!
I have a Mikrotik DHCP relay server that has more than one dhcp servers on it with different scopes. This is a main Mikrotik router with all the scopes but there are other routers, each has only one scope. I am trying to automate DHCP static leases export and import to other Mikrotik routers for failover purposes.
I have a script that exports all the static leases in a file:
/ip dhcp-server lease export file=leases.rsc
I can run this exported file on another Mikrotik router, but the problem is, that this file contains leases for all the scopes and DHCP servers.
The file fails to run on a backup Mikrotik with error: script error: input does not match any value of server
How can I export only needed leases to a leases.rsc file?
Please help me with this challenge.
Thanks!
I had an idea to export only those static leases that have “server 1” in their server field.
Is that possible?
I tried to use “find” argument, but nothing is exporting to a file.
Can someone who is good at RouterOS scripting help me?
how about this?
:exec "\
/ip dhcp-server lease;\
:local leases [find where !dynamic server=\"server 1\"];\
:put \"/ip dhcp-server lease\";\
:foreach i in=\$leases do={\
:local addr [get \$i \"address\"];\
:local clid [get \$i \"client-id\"];\
:local macd [get \$i \"mac-address\"];\
:local serv [get \$i \"server\"];\
:put \"add address=\$addr client-id=\$clid mac-address=\$macd server=\\\"\$serv\\\"\";\
}\
" file=leases
check output with:
/file get leases.txt contents
import backup file:
import file=leases.txt
Thanks! That script worked perfectly!