I want to have my DHCP leases to be registered in the local DNS cache.
To prepare you put a comment into the DHCP lease which will be treated as the client DNS name. If comment is missing then DNS name will be set to client id (which would depend on client OS)
Use the following script to register the DHCP (I got it from a Mikrotik forum, and adjusted so that comment takes precedence over client id)
: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 $comment ] > 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
}
/
}
You create a scipt, call it dyndns, paste the source above. Then within your dhcp server you set is as the lease script
/ip dhcp-server set lease-script=dyndns dhcp1