Script for synchronization of DHCP leases between master and slaves

Hello, i have written a dhcp-lease script that converts any new lease to static and sends it to a slave (extendable for multiple slaves). No script is required on the slave, everything runs on master as a dhcp-lease script. I had to workaround limitation of using variables in ssh-exec, so i had to use temp files as a helper (export of a single lease is also not possible).

My intention was to avoid removing all of the leases on the slave router and reimporting them from master every time (as it should be more reliable in edge cases for example power loss during import etc). I also do not like idea of removing and reimporting everything, when you only need to add a new one. Another advantage of this method is, that lease is transferred to slaves as soon as it is created, not in scheduler every X minutes.

So here is the script, please tell me what you think and where it can be improved. Notice the part where i wait for the file to be actually available on the FS - i have found many threads where people cannot write to a newly created file in a script, just because mikrotik does not wait for the file to be created before executing next command. If you have better way for this, just let me know.

I am planning to share it on GitHub if you guys find it useful.

#
# This script converts any new DHCP leases to static automatically and transfers them to the failover router
#
# Base for this script was taken from dynamic to static lease script by Jotne <jo.overland at gmail.com>
# http://forum.mikrotik.com/t/dhcp-automatic-dynamic-to-static/129147/1
#
# Author Lukáš Krejza <gryffus@hkfree.org>
#
#
###########################################

#
# Config:
#

# IP address of the slave router
:local failoverIP 127.0.0.1

# Login and password to the failover router

# Username (ftp) on slave router
:local failoverUsername admin

# Password (ftp) on slave router
:local failoverPassword admin

# Local and remote lease file names
#
# Local will have rsc.txt extension
# Remote will have .auto.rsc extension
#
# Both filenames will be prepended with MAC address of the client
#
# Sent files are removed, received files are kept, so cleaning
# needs to be done on the slave.
#

# Fixed part of temp file names on master router (local)
:local localLeaseFilename lease

# Fixed part of temp file names on slave router (remote)
:local remoteLeaseFilename lease

###########################################

# 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

# Generate file name
	:local localLeaseFile ($leaseActMAC."-".$localLeaseFilename.".rsc.txt")
	:local remoteLeaseFile ($leaseActMAC."-".$remoteLeaseFilename.".auto.rsc")

# Create new lease import file
	if ([:len [/file find name=$localLeaseFile]]>0) do={/file remove $localLeaseFile}
	/file print file=$localLeaseFile

# Wait for the file to be available on FS
	:while ([:len [/file find name=$localLeaseFile]]<1) do={ :delay 1 };

# Write import string for the lease to the file
	/file set $localLeaseFile contents="/ip dhcp-server lease add address=$leaseActIP mac-address=$leaseActMAC comment=\"From Primary: $date $time $Name\" server=$leaseServerName"

# Transfer and import lease file to slave (automatic import works only via FTP and file name *.auto.rsc)
	/tool fetch address=$failoverIP src-path=$localLeaseFile dst-path=$remoteLeaseFile user=$failoverUsername password=$failoverPassword mode=ftp upload=yes

# Cleanup after ourselves
	if ([:len [/file find name=$localLeaseFile]]>0) do={/file remove $localLeaseFile}

# Send a message to the log	
	:log info message="script=dhcp_lease server=$leaseServerName IP=$leaseActIP MAC=$leaseActMAC name=$Name"
}

Most of the work is just a copy from this: http://forum.mikrotik.com/t/dhcp-automatic-dynamic-to-static/129147/1
That is ok, but giving the original creator some credit, or just a link to the original post would be fine :slight_smile:

Edited. I forgot where i got the basis, thanks for letting me know.