Skip to content

Instantly share code, notes, and snippets.

@dgholz
Last active November 24, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgholz/36543905cd0dc872f29eb207feb0eb52 to your computer and use it in GitHub Desktop.
Save dgholz/36543905cd0dc872f29eb207feb0eb52 to your computer and use it in GitHub Desktop.
RouterOS script to create/delete DNS entries to match DHCP leases. Set it as your lease-script on your DHCP server.
/ip dhcp-server network
:local domain "default"
:do { :set domain [ get [ find where $leaseActIP in address ] domain ] } on-error={}
/ip dhcp-server
:local ttl [ get [ find where name=$leaseServerName ] lease-time ]
/ip dhcp-server lease
:local name
:do { :set name "$[ get [ find where server=$leaseServerName && address=$leaseActIP && mac-address=$leaseActMAC ] host-name ].$domain" } on-error={}
# calling this 'comment' makes the find where comment=$comment match all entries...
:local forLease "from $leaseServerName for $leaseActMAC"
# the namespace switch doesn't apply to the foreach (it looks at leases if the namespace is removed from the find)
/ip dns static
:foreach entry in=[ /ip dns static find where comment=$forLease] do={
:if ( $leaseBound = "1" ) do={
:if ( [ get $entry name ] != $name || [ get $entry address ] != $leaseActIP ) do={
/log info "removing old DNS entry for $name"
remove $entry
}
} else={
/log info "removing DNS entry for $leaseActIP"
remove $entry
}
}
:if ( $leaseBound = "1" && [ :len [ find where comment=$forLease ] ] = "0" ) do={
/log info "adding DNS entry for $name"
add name=$name address=$leaseActIP ttl=$ttl comment=$forLease
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment