dtype.org
Log in

Mikrotik configs

From dtype.org
Revision as of 02:29, 6 October 2021 by Drew (talk | contribs) (DHCP to DNS by cron)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

DHCP to DNS 1

From: https://blog.pessoft.com/2019/09/06/mikrotik-script-automatic-dns-records-from-dhcp-leases/

# When "1" all DNS entries with IP address of DHCP lease are removed
:local dnsRemoveAllByIp "1"
# When "1" all DNS entries with hostname of DHCP lease are removed
:local dnsRemoveAllByName "1"
# When "1" addition and removal of DNS entries is always done also for non-FQDN hostname
:local dnsAlwaysNonfqdn "1"
# DNS domain to add after DHCP client hostname
:local dnsDomain "dynamic.example.local"
# DNS TTL to set for DNS entries
:local dnsTtl "00:15:00"
# Source of DHCP client hostname, can be "lease-hostname" or any other lease attribute, like "host-name" or "comment"
:local leaseClientHostnameSource "lease-hostname"

:local leaseComment "dhcp-lease-script_$leaseServerName_$leaseClientHostnameSource"
:local leaseClientHostname
:if ($leaseClientHostnameSource = "lease-hostname") do={
  :set leaseClientHostname $"lease-hostname"
} else={
  :set leaseClientHostname ([:pick \
    [/ip dhcp-server lease print as-value where server="$leaseServerName" address="$leaseActIP" mac-address="$leaseActMAC"] \
    0]->"$leaseClientHostnameSource")
}
:local leaseClientHostnames "$leaseClientHostname"
:if ([:len [$dnsDomain]] > 0) do={
  :if ($dnsAlwaysNonfqdn = "1") do={
    :set leaseClientHostnames "$leaseClientHostname.$dnsDomain,$leaseClientHostname"
  } else={
    :set leaseClientHostnames "$leaseClientHostname.$dnsDomain"
  }
}
:if ($dnsRemoveAllByIp = "1") do={
  /ip dns static remove [/ip dns static find comment="$leaseComment" and address="$leaseActIP"]
}
:foreach h in=[:toarray value="$leaseClientHostnames"] do={
  :if ($dnsRemoveAllByName = "1") do={
    /ip dns static remove [/ip dns static find comment="$leaseComment" and name="$h"]
  }
  /ip dns static remove [/ip dns static find comment="$leaseComment" and address="$leaseActIP" and name="$h"]
  :if ($leaseBound = "1") do={
    :delay 1
    /ip dns static add comment="$leaseComment" address="$leaseActIP" name="$h" ttl="$dnsTtl"
  }
}

DHCP to DNS by cron

Thanks to Daniel Aleksandersen for the article at https://www.ctrl.blog/entry/routeros-dhcp-lease-script.html for the start to this.

:local domains [:toarray "m"]
:local dnsttl "10m"

:local magiccomment "automatic-from-dhcp (magic comment)"
:local activehosts [:toarray ""]

:foreach lease in [/ip dhcp-server lease find] do={
  :local hostname [/ip dhcp-server lease get value-name=comment $lease]
  :if ([:len $hostname] < 1) do={
    :set hostname [/ip dhcp-server lease get value-name=host-name $lease]
  }
  :local hostaddr [/ip dhcp-server lease get value-name=active-address $lease]

  :if (([:len $hostname] > 0) && ([:len $hostaddr] > 0)) do={
    :foreach domain in $domains do={
      :local regdomain "$hostname.$domain"
      :set activehosts ($activehosts, $regdomain)

      :if ([:len [/ip dns static find where name=$regdomain]] = 0) do={
        /ip dns static add name=$regdomain address=$hostaddr comment=$magiccomment ttl=$dnsttl
      } else={
        :if ([:len [/ip dns static find where name=$regdomain comment=$magiccomment]] = 1) do={
          :if ([/ip dns static get value-name=address [/ip dns static find name=$regdomain comment=$magiccomment]] != $hostaddr) do={
            /ip dns static set address=$hostaddr [/ip dns static find name=$regdomain comment=$magiccomment]
          }
        }
      }
    }
  }
}

:foreach dnsentry in [/ip dns static find where comment=$magiccomment] do={
  :local hostname [/ip dns static get value-name=name $dnsentry]
  :if ([:type [:find $activehosts $hostname]] = "nil") do={
    /ip dns static remove $dnsentry
  }
}