I did search for a script that did convert dynamic lease to static lease, but did not find any good solution.
So I made a new script that is placed in the DHCP server settings and convert the dynamic IP to static IP.
Cut and past to:
IP->DHCP-Server->Select your dhcp server->Lease Script
PS this makes all IP static, no expedition.
# Created Jotne 2021 v1.4
#
# This scripts converts all DHCP release to static automatically
# It should run on all routerOS version
/ip dhcp-server lease
# Test if this is a Bound session and the lease is a dynamic one. Do not change older reservation
:if (($leaseBound=1) && ([find where dynamic mac-address=$leaseActMAC]!="")) do={
# Get the lease number
:local Lease [find mac-address=$leaseActMAC]
# Get date and time
:local date [/system clock get date]
:local time [/system clock get time]
# Make the lease static
make-static $Lease
# Get host name
:local Name [get $Lease host-name ]
# Add date and time as a comment to show when it was seen first time
comment comment="$date $time $Name" $Lease
# Send a message to the log
:log info message="script=dhcp_static server=$leaseServerName IP=$leaseActIP MAC=$leaseActMAC name=$Name"
}
This script deletes all static DHCP entry that has not been seen last 100 weeks (can be adjusted). At the same time it also delete the DNS entry corresponding to DHCP lease that is removed.
# Created Jotne 2021 v1.2
# Remove all static DHCP and corresponding DNS leases more than 100 week old
# 1.1 Used simpler regex
# 1.2 Made more dynamic
# Set how old data you like to delete. E.g older than 100 week ->"100w". Older than 30 days -> "30d"
:local delete "100w"
# Find and delete entries
/ip dhcp-server lease
:foreach id in=[find where dynamic=no last-seen>[:totime $delete]] do={
:local ip [get $id address]
:local hostdhcp [get $id host-name]
:local hostdns "N/A"
# Remove DNS entry if exist
:do {
:set $hostdns [/ip dns static get [find where address=$ip] name]
/ip dns static remove [find where address=$ip]
} on-error={ }
# remove DHCP entry
remove $id
# Send log message
:log info message="script=dhcp_clean older_than=$delete ip=$ip host_dhcp=\"$hostdhcp\" host_dns=\"$hostdns\""
}