DHCP automatic dynamic to static

DHCP dynamic to static lease

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\""
}

Thank you this script is useful

How about just extending the DHCP lease time to one month, so unless your device is offline for a month it will retain its IP address (and if you have lots of guests then setup a bigger subnet).

excellent script, works great
Could you add in comment, the host name that appears from the lease?

Host name will stay in the lease as host-name field.

But if you like to add it in the comment field as well, this should do:


# Created Jotne 2020 v1.3
#
# This scripts converts all DHCP release to static automatically
# It should run on all routerOS version


# Test if this is a Bound session and the lease is a dynamic one. Do not change older reservation
:if (($leaseBound=1) && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do {

# Get the lease number
	:local Lease [/ip dhcp-server 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	
	/ip dhcp-server lease make-static $Lease

# Get host name
	:local Name [/ip dhcp-server lease get $Lease host-name ] 
	
# Add date and time as a comment to show when it was seen first time	
	/ip dhcp-server lease 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"
}

Hello,

Thanks for your information.

You saved me with “:local Lease [/ip dhcp-server lease find mac-address=$leaseActMAC]”. I am building a DHCP script that make static MACs inside allowed list file, and disable the ones that are not listed (in a csv file), then send a mail informing whats happened.

See you.

Alex

Since converting all DHCP lease to static will fill opp the DHCP scope over time, I have created a script that delete all static DHCP entry that has not been seen last 100 week. At the same time it also delete the DNS entry corresponding to DHCP lease that is removed.

# Remove all static DHCP and corenspondig DNS leases more than 100 week old
:local counter 0
/ip dhcp-server lease
:foreach id in=[find where dynamic=no last-seen~"^1[0-9][0-9]"] do={
	:local ip [get $id address]
	:set counter ($counter+1)
	# delete DNS entry with that IP
	/ip dns static remove [find where address=$ip]
	# remove DHCP entry
	remove $id
}
:log info message="Number of static DHCP older>100weeks removed: $counter"

the problem is when last-seen is “never” (like on reboot)

On my test router last-seen remain correct after reboot and upgrade to 6.48.4
I do not have any never on the 750Gr3 router.

Here is the current list of last-seen on the router:

19h39m28s, 5w1d12h14m17s, 9h22m3s, 5w1d12h57m55s, 98w3d12h36m58s, 29w1d13h12m53s, 87w5d14h34m39s, 53w4d9h38m51s, 1d14h24m8s, 11h19m44s, 77w3d20h24m55s, 4w4d15h36m, 33w5d2h13m17s, 9w2d6h11m40s, 36w3d13h29m10s, 43w3d16h30m39s, 14w2d18h45m59s, 68w6d16h9m18s, 92w4d12h48m53s, 78w3d14h54m20s, 70w6d2h15m5s, 64w4d13h11m49s, 98w2d13h4m1s, 52w3d10h4m47s, 43w7h36m50s, 62w4d19h47m49s, 25w5d4h8m45s, 95w5d16h44m55s, 87w6d12h45m13s, 17w5d18h26m37s, 15w2d13h40m29s, 55w4d21h50m50s, 55w5d2h41m27s, 55w1d14h40m1s, 49w14h36m12s, 25w13h1m14s, 53w3d14h4m34s, 27w4d8h46m26s, 81w2d12h26m53s, 82w5d16h18m42s, 62w6d11h5m34s, 62w4d14h19m19s, 65w17h23m24s, 5w1d12h14m33s, 86w3d19h29s, 96w8h59m47s, 93w1d13h43m43s, 35w6d14h16m58s, 98w3d12h12m40s, 61w6d11h46m5s, 9h8m56s, 97w3d17h3m15s, 71w3d17h18m27s, 31w2d13h43m48s, 95w4d13h8m12s, 23w10h52m44s, 95w1d9h45m54s, 42w2d22h44m50s, 70w2d16h46m51s, 83w1d10h42m56s, 88w4d20h41m26s, 36m52s, 89w5d6h13m23s, 87w6d13h54m41s, 5w10h35m43s, 52w3d12h12m3s, 71w2d14h21m35s, 71w2d11h18m51s, 47w1d11h35m40s, 53w3d11h58m35s, 45w4d7h7m30s, 18h2m7s, 77w3d21h31m46s, 77w3d19h47m52s, 14h57m58s, 79w6d11h13m2s, 63w1d16h45m, 79w17h8m14s, 77w6d9h43m25s, 7h53m57s, 75w3d14h44m5s, 40w1d10h49m12s, 42w5d13h32m57s, 42w4d14h40m45s, 53w3d12h12m42s, 69w2d13h9s, 69w2d11h23m12s, 66w6d21h33m45s, 69w1d11h54m39s, 69w1d11h30m9s, 68w3d15h49m28s, 68w3d15h53m55s, 29w5d16h18m4s, 30w20h16m43s, 32w4d6h27m9s, 28w6d19h1m54s, 53w4d6h32m41s, 17w1d17h23m21s, 48w5d16h46m8s, 53w4d8h33m1s, 6d12h52m15s, 61w3d14h58m, 61w3d14h53m51s, 55m30s, 61w3h15m23s, 58w3d15h18m36s, 7w3d10h48m5s, 53w4d8h9m36s, 42w8h17m16s, 53w4d11h44m7s, 53w4d9h47m23s, 53w3d13h7m48s, 52w5d18h36m8s, 31w2d9h30m59s, 45w5d12h28m37s, 49w1d9h34m20s, 4w3d17h10m50s, 4w3d20h36m17s, 7h44m26s, 13h49m44s, 29w3d20h26m2s, 32w5d4h31m30s, 4w5d9h12m41s, 4w6d14h38m, 6d23h56m40s, 48w15h55m33s, 2w3d20h25m52s, 3w17h12m38s, 48w14h14m51s, 13w3d10h36m6s, 44w4d12h48m9s, 22w1d12h56m27s, 32w5d6h8m57s, 23w5d15h32m25s, 9w3d17h29m55s, 15h30m54s, 12w3d16h27m24s, 30w5d5h47m7s, 1w2d16h1m23s, 16w5d2h55m49s, 1d14h41m36s, 34w6d16h48m47s, 1h19m22s, 40w4d10h55m46s, 34w5d16h31m45s, 16w4d3h53m13s, 1w8h44m4s, 1d19h43m40s, 38w15h29m46s, 8w5d9h22m2s, 7w3d10h36m32s, 35w2d5h42m52s, 36w2d3h14m48s, 13w3d11h34m4s, 36w1d12h34m2s, 4w5d18h24m25s, 6d21h14m46s, 15w3d7h44m21s, 11w4d6h40m17s, 28w2d14h42m42s, 32w22h39m49s, 20w5d8h21m22s, 31w2d13h41m32s, 30w5d23h59m22s, 13w2d9h47m52s, 29w5d16h16m58s, 13h27m5s, 6d12h42m48s, 19w3d8h49m4s, 1d11h54m17s, 28w6d17h7m14s, 28w6d14h7m54s, 22w2d20h32m14s, 28w15h7m34s, 11w2d23h11m23s, 27w4d9h58m, 27w4d5h40m56s, 17w5d12h38m51s, 11h45m5s, 25w4d20h22m43s, 22m33s, 9h34m54s, 24w5d16h5m57s, 2d8h25m59s, 12w4d13h9m14s, 3w1d16h41m3s, 22w5d19h15m32s, 22w5d17h10m49s, 22w5d17h45m57s, 12w4d16h21m49s, 11w18h58m52s, 13w2d10h28m1s, 16w1d15h26m18s, 19w4d7h7m25s, 1d14h41m58s, 4w5d8h30m17s, 17w9h20m15s, 16w5d8h53m48s, 16w4d19h57m51s, 12w5d10h24m56s, 14w3d11h51m45s, 7w3d10h34m24s, 13w11h47m9s, 1d16h42m33s, 12w1d13h43m53s, 8w2d12h58m39s, 9w6d16h58s, 11w2d11h29m10s, 10w9h49m28s, 7w3d10h16m56s, 2w4d7h15m21s, 13h38m23s, 7w4d13h8m49s, 2w5d23h8m28s, 4d9h54m, 7w4d13h11m33s, 3w3d17h41m25s, 7w3d12h9m7s, 6w3d16h32m30s, 5w1d4h55m, 5w4d11h48m28s, 8h43m6s, 4w6d21h41m, 29m17s, 1w5d12h8m59s, 1d13h47m37s, 1d12h46m41s

Script updated to 1.4 (in fist post)
Just some clean up and code shortening.

For rapidly simulating “never”, simply add manually a static lease.

That is ok behavior. For me, if some is added manually, it should be removed manually.
All IP added by the script has last seen, as far as I see.

Google Translate:
hi, thank you for the script, please be able to script some condition in the sense of the data. delete only if there is some text in the comment?, or IP range?
in all the attempts I made, the script deletes IP addresses which, although added as a DHCP lease but subsequently changed to static, it is very important for me that these IP addresses are not lost,
for my attempt, I added to your script everything you delete, put it in the log .. and I was horrified by how many unwanted ones (for me, important IPs, it deleted ..)

like this ( :log info message=“Number of static DHCP older>100weeks removed: $ip” )
Number of static DHCP older> 100weeks removed: 192.168.3.125
Number of static DHCP older> 100weeks removed: 192.168.32.31
Number of static DHCP older> 100weeks removed: 192.168.32.4
Number of static DHCP older> 100weeks removed: 172.16.0.54
Number of static DHCP older> 100weeks removed: 10.1.1.4


and more…
I know that in this range 192.168.0.0/16 it is undesirable for me to delete anything

many thanks four your work


original lang:
ahoj, děkuji za script, byla by prosím možnost do scriptu dat nejakou podminku ve smyslu. smaze pouze pokud je v komentari nejaky text ?, popripade IP rozsah ?
ve vsech pokusech ktere jsem udelal, mi script maze IP adresy ktere, byly pridany sice jako DHCP lease ale nasledne zmeneny na statick, je pro me velice dulezite aby se tyto IP adresy neztraceli,
pro muj pokus jsem do Vaseho scriptu pridal vsechno co smazes dej do logu.. a byl jsem zdesen kolik nezadoucich ( pro me dulezitych IP to smazalo.. )

vím ze v rozsahu 192.168.0.0/16 je pro me nezadouci cokoliv mazat

like this ( :log info message=“Number of static DHCP older>100weeks removed: $ip” )
Number of static DHCP older>100weeks removed: 192.168.3.125
Number of static DHCP older>100weeks removed: 192.168.32.31
Number of static DHCP older>100weeks removed: 192.168.32.4
Number of static DHCP older> 100weeks removed: 172.16.0.54
Number of static DHCP older> 100weeks removed: 10.1.1.4

a dalsich..

nahradit název serveru DHCP, ze kterého nechcete, aby byl odstraněn:


# Remove all static DHCP and corenspondig DNS leases more than 100 week old
:local counter 0
/ip dhcp-server lease
:foreach id in=[find where server!="<dhcp-server-name>" and dynamic=no and last-seen~"^1[0-9][0-9]\$"] do={
	:local ip [get $id address]
	:set counter ($counter + 1)
	# delete DNS entry with that IP
	/ip dns static remove [find where address=$ip]
	# remove DHCP entry
	remove $id
}
:log info "Number of static DHCP older > 100 weeks removed: $counter"

Hi!

Do you know if in ROS 7.X (7.5 specifically) something has changed in the syntax, referring to the following query?

/ip dhcp-server lease
:if (($leaseBound=1) && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do= {
:do {
<...>
}

Thanks & BR.

try first correct syntax…

do not compare “empty string” with “empty array”, “array with single id” or “array of id”…
(find return one array, and that array can be empty, with only one element, or with more elements inside, and “” is a empty string)

do not compare number with string ($leaseBound is a string, yes is counterintuitive)

“where dynamic mac-address” where is the “and”?

“where dynamic” what???
dynamic can be yes or no (string), not a true / false

“do= {” no space between = and {

/ip dhcp-server lease
:if ( ($leaseBound = “1”) and ([:len [find where dynamic=no and mac-address=$leaseActMAC]] > 0) ) do={

}

This script is from Jotne (first post) and I’ve had it running until I upgraded to ROS 7.5.
Thank you Rextended for your invaluable help.

BR.

Do you say that the original script in post #1 does not work in 7.5

where to put your second script ****

This script deletes all static DHCP

?

Did you read the line? You just did quote part of it.

This script deletes all static DHCP entry that has not been seen last 100 weeks (can be adjusted).

Bottom script is to clean up your DHCP, so if no one uses a lease for more than 100 weeks, then just remove it.
No need for static leases that are not used.