Last active
October 17, 2024 06:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]; | |
}; |
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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: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