Files
mikrotik/ static_dns_for_dhcp/DHCP2DNS_FULLUPDATE

29 lines
1.2 KiB
Plaintext

:foreach lease in=[/ip dhcp-server lease find] do={
:local hostName [/ip dhcp-server lease get $lease host-name]
# Skip if hostName is empty
:if ( [ :len $hostName ] > 0 ) do={
:local ipAddress [/ip dhcp-server lease get $lease address]
:local ttl [/ip dhcp-server lease get $lease expires-after]
# Find the corresponding network for the lease's IP address
:local networkId [/ip dhcp-server network find where $ipAddress in address]
:local domainName [/ip dhcp-server network get $networkId domain]
:local fqdn "$hostName.$domainName"
:log info "Processing $fqdn with IP $ipAddress and TTL $ttl"
# Remove any existing static DNS entries with the same IP address
:foreach oldRecord in=[/ip dns static find where address=$ipAddress and comment~"#DHCP"] do={
:log info "Removing old DNS entry with IP $ipAddress"
/ip dns static remove $oldRecord
}
# Add or update the static DNS entry
/ip dns static add address=$ipAddress name=$fqdn ttl=$ttl comment="#DHCP" disabled=no
}
}
/system script run DHCP2DNS_CLEANUP
/system script run NATUPDATE