-
-
Save SmartFinn/acc7953eaeb43cece034 to your computer and use it in GitHub Desktop.
# MikroTik (RouterOS) script for automatically setting DNS records | |
# for clients when they obtain a DHCP lease. | |
# | |
# author SmartFinn <https://gist.github.com/SmartFinn> | |
:local dnsTTL "00:15:00"; | |
:local token "$leaseServerName-$leaseActMAC"; | |
# Normalize hostname (e.g. "-= My Phone =-" -> "My-Phone") | |
# - truncate length to 63 chars | |
# - substitute disallowed chars with a hyphen | |
# param: name | |
:local normalizeHostname do={ | |
:local result; | |
:local isInvalidChar true; | |
:for i from=0 to=([:len $name]-1) do={ | |
:local char [:pick $name $i]; | |
:if ($i < 63) do={ | |
:if ($char~"[a-zA-Z0-9]") do={ | |
:set result ($result . $char); | |
:set isInvalidChar false; | |
} else={ | |
:if (!$isInvalidChar) do={ | |
:set result ($result . "-"); | |
:set isInvalidChar true; | |
}; | |
}; | |
}; | |
}; | |
# delete trailing hyphen | |
:if ($isInvalidChar) do={ | |
:set result [:pick $result 0 ([:len $result]-1)]; | |
} | |
:return $result; | |
}; | |
:if ($leaseBound = 1) do={ | |
# Getting the hostname and delete all disallowed chars from it | |
:local hostName $"lease-hostname"; | |
:set hostName [$normalizeHostname name=$hostName]; | |
# Use MAC address as a hostname if the hostname is missing or contains only | |
# disallowed chars | |
:if ([:len $hostName] = 0) do={ | |
:set hostName [$normalizeHostname name=$leaseActMAC]; | |
}; | |
# Getting the domain name from DHCP server. If a domain name is not | |
# specified will use hostname only | |
/ip dhcp-server network { | |
:local domainName [get [:pick [find $leaseActIP in address] 0] domain]; | |
:if ([:len $domainName] > 0) do={ | |
# Append domain name to the hostname | |
:set hostName ($hostName . "." . $domainName); | |
}; | |
}; | |
:do { | |
/ip dns static { | |
add name=$hostName address=$leaseActIP ttl=$dnsTTL comment=$token; | |
}; | |
} on-error={ | |
:log error "Fail to add DNS entry: $hostname -> $leaseActIP ($leaseActMAC)"; | |
}; | |
} else={ | |
/ip dns static remove [find comment=$token]; | |
}; |
@velosol sadly but nor $lease-hostname
nor even $leaseHostname
are not passed to lease-script.
@SmartFinn You have to use $"lease-hostname"
(with the quotes) - works on 6.47.8.
I set it locally to $leaseHostName
to make things look nice 😉
@elevendroids yeah, it works but only when you put the whole script to lease-script or with a wrapper in lease-script. It won't work when you call a script from /system script
in lease-script that useful in a case with multiple DHCP servers.
@SmartFinn You don't have to do that.
Just put the name of the script in the lease-script
field - calling /system script run dhcp2dns
won't do the trick:
/ip dhcp-server
add address-pool=vlan99-pool disabled=no interface=vlan99 lease-script=dhcp2dns name=vlan99-dhcp
add address-pool=vlan10-pool disabled=no interface=vlan10 lease-script=dhcp2dns name=vlan10-dhcp
This way the script will be called directly with the internal env variables correctly passed to it - no need for wrapper scripts or other nonsense 😉
Unfortunately this is not clearly documented - I've found this method somewhere on the Mikrotik forums...
Works fine on RB4011 with RouterOS 6.47.8
You can check out my version of a similar script here: https://github.com/elevendroids/mikrotik-scripts/blob/master/dhcp2dns.rsc
@elevendroids I was just talking about this method. It doesn't work on ROS 6.48:
@SmartFinn - use $"lease-hostname"
not $"host-name"
@elevendroids right, my fault.
Is there a reason to get the host name as in line 29 versus just using the lease-script variable $lease-hostname ?