Community discussions

MikroTik App
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 1628
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

DHCP automatic dynamic to static

Wed Apr 03, 2019 3:41 pm

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 2019 v1.2
#
# 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
	
# Add date and time as a comment to show when it was seen first time	
	/ip dhcp-server lease comment comment="$date $time" $Lease
	
# Send a message to the log	
	:log info message="script=dhcp_static server=$leaseServerName IP=$leaseActIP MAC=$leaseActMAC"
}

If you like to start with converting all current dynamic IP to static in one go, run this from command line.
[
:local date [/system clock get date]
:local time [/system clock get time]
:foreach Lease in=[/ip dhcp-server lease find dynamic] do={
	/ip dhcp-server lease make-static $Lease
	:local Name [/ip dhcp-server lease get $Lease host-name] 
	/ip dhcp-server lease comment comment="$date $time $Name" $Lease
}
]
Last edited by Jotne on Sat Apr 25, 2020 11:08 pm, edited 4 times in total.
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 
 
bigmayo
just joined
Posts: 1
Joined: Sat Jun 22, 2019 9:00 am

Re: DHCP automatic dynamic to static

Sun Jan 12, 2020 12:58 pm

Nice script so far, but at:

# Send a message to the log
:log info message="script=dhcp_static server=$leaseServerName IP=$leaseActIP MAC=$leaseActMAC"

I just get this in log:

script=dhcp_static server= IP= MAC=

any idea why the server ip and mac keeps empty?
 
sopyan0807
newbie
Posts: 40
Joined: Wed Jan 22, 2020 5:21 pm
Location: Indonesia
Contact:

Re: DHCP automatic dynamic to static

Sat Jan 25, 2020 8:19 am

Thank you this script is useful
 
User avatar
Joni
Frequent Visitor
Frequent Visitor
Posts: 85
Joined: Fri Mar 20, 2015 2:46 pm
Contact:

Re: DHCP automatic dynamic to static

Sat Jan 25, 2020 6:30 pm

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).
 
arielf
newbie
Posts: 38
Joined: Fri May 09, 2008 4:58 pm

Re: DHCP automatic dynamic to static

Sat Apr 25, 2020 7:24 pm

excellent script, works great
Could you add in comment, the host name that appears from the lease?
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 1628
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: DHCP automatic dynamic to static

Sat Apr 25, 2020 10:35 pm

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"
}
 
How to use Splunk to monitor your MikroTik Router(s)

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 21 guests