Hi!
I would like to ask for help with the following script.
The goal would be to retrieve static DHCP leases from another router and then process them and load them into the router where we want them.
So for example, CHR1 is the main device and CHR2 is where we want to migrate the data to.
Do you have any ideas when using the JSON Parse :parse command how to ignore or delete the first field “.id”, as this is why the :parse command cannot properly process the incoming data and the process cannot proceed, it just tries to create a DHCP lease with empty data.
Example code:
:local result [/tool fetch url="http://dummyuser:dummypassword@1.1.1.1/rest/ip/dhcp-server/lease" as-value output=user]
:if ($result->"status" = "finished") do={
:local response ($result->"data")
:log info ("OK, response: " . $response)
} else={
:log error ("Something went wrong. Status: " . ($result->"status"))
}
:local leaseList [:parse $response]
:log warning ("leaseList: $leaseList")
:log warning ($response)
:local existingLeases [/ip dhcp-server lease print as-value]
:foreach lease in=$leaseList do={
:local newAddress ($lease->"address")
:local newMac ($lease->"mac-address")
:local newServer ($lease->"server")
:local newComment ($lease->"comment")
:local newLeaseTime ($lease->"lease-time")
:local existingLease [/ip dhcp-server lease find where address=$newAddress and mac-address=$newMac]
:if ([:len $existingLease] = 0) do={
/ip dhcp-server lease add address=$newAddress mac-address=$newMac server=$newServer disabled=no comment="Sync from other router"
:log warning ("New DHCP lease has been added successfully: " . $newAddress . " - " . $newMac)
} else={
:log info ("The DHCP lease already exists: " . $newAddress . " - " . $newMac)
}
}
:log info "DHCP lease records synchronization is complete."
Example data:
[
{
".id":"*2",
"address":"192.168.88.253",
"address-lists":"",
"blocked":"false",
"dhcp-option":"",
"disabled":"false",
"dynamic":"false",
"last-seen":"never",
"lease-time":"30m",
"mac-address":"8F:FD:7E:8F:29:C1",
"radius":"false",
"server":"dhcp1",
"status":"waiting"
},
{
".id":"*6",
"address":"192.168.88.1",
"address-lists":"",
"blocked":"false",
"dhcp-option":"",
"disabled":"false",
"dynamic":"false",
"last-seen":"never",
"mac-address":"A0:BF:8C:2C:9A:0E",
"radius":"false",
"server":"dhcp1",
"status":"waiting"
},
{
".id":"*7",
"address":"192.168.88.2",
"address-lists":"",
"blocked":"false",
"dhcp-option":"",
"disabled":"false",
"dynamic":"false",
"last-seen":"never",
"mac-address":"E8:B8:6A:98:AA:31",
"radius":"false",
"server":"dhcp1",
"status":"waiting"
}
]