The script registers only fully qualified domain names (FQDN). Host part of the registering FQDN is the value of the "host-name" property of the lease or the "comment" property if the "host-name" is empty. Domain part is the value of the "domain" property of the corresponding DHCP server network. The script doesn't register DNS entries for invalid FQDNs with empty or undefined host or domain parts. Also the script doesn't register DNS entry when the entry with the same IP address or the FQDN already exists in the router's static DNS database.
Static DNS entries, managed by the script, have the tag #DHCP as a comment to distinguish them from the manually created ones.
The TTL of registering DNS entry is equal to the TTL of the corresponding DHCP lease.
On the DHCP lease expiration the script removes corresponding DNS entry with the same IP address, tagged as #DHCP in the comment.
The script logs various errors and informational messages with the tag DHCP2DNS. The logged messages are self-explanatory.
The script should be created as the standard system script. For the script to run for the given DHCP server, it's name should be assigned to the "lease-script" property of this server.
Source code follows.
:local DHCPtag
:set DHCPtag "#DHCP"
:if ( [ :len $leaseActIP ] <= 0 ) do={ :error "empty lease address" }
:if ( $leaseBound = 1 ) do=\
{
:local ttl
:local domain
:local hostname
:local fqdn
:local leaseId
:local comment
/ip dhcp-server
:set ttl [ get [ find name=$leaseServerName ] lease-time ]
network
:set domain [ get [ find $leaseActIP in address ] domain ]
.. lease
:set leaseId [ find address=$leaseActIP ]
# Check for multiple active leases for the same IP address. It's weird and it shouldn't be, but just in case.
:if ( [ :len $leaseId ] != 1) do=\
{
:log info "DHCP2DNS: not registering domain name for address $leaseActIP because of multiple active leases for $leaseActIP"
:error "multiple active leases for $leaseActIP"
}
:set hostname [ get $leaseId host-name ]
:set comment [ get $leaseId comment ]
/
:if ( [ :len $hostname ] <= 0 ) do={ :set hostname $comment }
:if ( [ :len $hostname ] <= 0 ) do=\
{
:log error "DHCP2DNS: not registering domain name for address $leaseActIP because of empty lease host-name or comment"
:error "empty lease host-name or comment"
}
:if ( [ :len $domain ] <= 0 ) do=\
{
:log error "DHCP2DNS: not registering domain name for address $leaseActIP because of empty network domain name"
:error "empty network domain name"
}
:set fqdn "$hostname.$domain"
/ip dns static
:if ( [ :len [ find name=$fqdn and address=$leaseActIP and disabled=no ] ] = 0 ) do=\
{
:log info "DHCP2DNS: registering static domain name $fqdn for address $leaseActIP with ttl $ttl"
add address=$leaseActIP name=$fqdn ttl=$ttl comment=$DHCPtag disabled=no
} else=\
{
:log error "DHCP2DNS: not registering domain name $fqdn for address $leaseActIP because of existing active static DNS entry with this name or address"
}
/
} \
else=\
{
/ip dns static
:local dnsDhcpId
:set dnsDhcpId [ find address=$leaseActIP and comment=$DHCPtag ]
:if ( [ :len $dnsDhcpId ] > 0 ) do=\
{
:log info "DHCP2DNS: removing static domain name(s) for address $leaseActIP"
remove $dnsDhcpId
}
/
}