Skip to content

Instantly share code, notes, and snippets.

@SmartFinn
Last active October 17, 2024 06:14
Show Gist options
  • Save SmartFinn/acc7953eaeb43cece034 to your computer and use it in GitHub Desktop.
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
# 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];
};
@johnmmcgee
Copy link

johnmmcgee commented Apr 2, 2019

Awesome script!

Only complaint is that if there is already a static entry for an IP and it doesn't match the pulled hostname, it will still add.

@walterzilla
Copy link

Runs smoothly in ROS 6.44.1 thanks!

@bezik46
Copy link

bezik46 commented Nov 8, 2020

6.47.7, all good, but worth checking this

@SmartFinn
Copy link
Author

@scerazy do you try to run the script directly from /system script? It doesn't work that way.

Place script in the lease script of the DHCP server you wish to assign DNS entries. Alternatively install as a script and call the script from the lease script.

@velosol
Copy link

velosol commented Dec 14, 2020

Is there a reason to get the host name as in line 29 versus just using the lease-script variable $lease-hostname ?

@SmartFinn
Copy link
Author

@velosol sadly but nor $lease-hostname nor even $leaseHostname are not passed to lease-script.

@elevendroids
Copy link

@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 😉

@SmartFinn
Copy link
Author

SmartFinn commented Dec 30, 2020

@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.

@elevendroids
Copy link

@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

@SmartFinn
Copy link
Author

@elevendroids I was just talking about this method. It doesn't work on ROS 6.48:
image

@elevendroids
Copy link

@SmartFinn - use $"lease-hostname" not $"host-name"

@SmartFinn
Copy link
Author

@elevendroids right, my fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment