Mikrotik dhcp to dns

Iš Žinynas.
16:02, 11 kovo 2023 versija, sukurta \dev\null (Aptarimas | indėlis) (Naujas puslapis: Scriptas skirtas automatiškai priskirti DNS įrašus naujiems DHCP lease'ams (klientams). Suteikiamas hostname su domain prefix'u (pvz.: '''iphone-x.clients.ofisas.lt'''...)
(skirt) ← Ankstesnė versija | Dabartinė versija (skirt) | Vėlesnė versija → (skirt)
Jump to navigation Jump to search

Scriptas skirtas automatiškai priskirti DNS įrašus naujiems DHCP lease'ams (klientams). Suteikiamas hostname su domain prefix'u (pvz.: iphone-x.clients.ofisas.lt), paimamas iš dhcp arba komentaro (jeigu jau yra statinis ip apibrėžimas). Taip yra patogiau atskirti vidinius tinklo resursus ir juos administruoti, intuityviau matomi klientai ir/ar jų tipai bei naudojamos sistemos.

Sudedame į > System > Scripts ir sudedame scriptą, užvadiname ir nustatome teises taip kaip parodyta paveikslėlyje.

Screenshot 2023-03-11 at 15.50.15.png

Įgaliname jį veikti po kiekvieno ip suteikimo klientui

IP > DHCP Server > (jūsų dhcp serveris) > Script įrašome jo pavadimimą, kaip parodyta paveikslėlyje.

Screenshot 2023-03-11 at 15.53.41.png

# updates can be found here https://forum.mikrotik.com/viewtopic.php?t=119469
:local DHCPtag   "#DHCP2DNS#"
:local LogPrefix "DHCP2DNS ($leaseServerName)"
:local domain "clients.xxx.lt"

###
# Functions

# remove \0 and spaces from string passed as inStr=<string>
:local trimString do=\
{
  :local outStr
  :for i from=0 to=([:len $inStr] - 1) do=\
  {
    :local tmp [:pick $inStr $i];
    :if (($tmp !=" ") and ($tmp !="\00")) do=\
    {
      :set outStr ($outStr . $tmp)
    }
  }
  :return $outStr
}

# "a.b.c.d" -> "a-b-c-d" for IP addresses used as replacement for missing host names
:local ip2Host do=\
{
  :local outStr
  :for i from=0 to=([:len $inStr] - 1) do=\
  {
    :local tmp [:pick $inStr $i];
    :if ($tmp =".") do=\
    {
      :set tmp "-"
    }
    :set outStr ($outStr . $tmp)
  }
  :return $outStr
}

###
# Script entry point
#
# Expected environment variables:
# leaseBound         1 = lease bound, 0 = lease removed
# leaseServerName    Name of DHCP server
# leaseActIP         IP address of DHCP client

:if ( [ :len $leaseActIP ] <= 0 ) do=\
{
  :log error "$LogPrefix: empty lease address"
  :error "empty lease address"
}

:if ( $leaseBound = 1 ) do=\
{
  # new DHCP lease added
  
  /ip dhcp-server
  :local ttl [ get [ find name=$leaseServerName ] lease-time ]
  network 
#  :local domain [ get [ find $leaseActIP in address ] domain ]
#  :set domain [ $trimString inStr=$domain ]

  .. lease
  :local 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 warning "$LogPrefix: Multiple active DHCP leases for '$leaseActIP' (???)"
    :error "Multiple active DHCP leases for '$leaseActIP' (???)"
  }  
  :local hostname [ get $leaseId host-name ]
  :set hostname [ $trimString inStr=$hostname ]

  :if ( [ :len $hostname ] <= 0 ) do=\
  {
    :set hostname [ $ip2Host inStr=$leaseActIP ]
    :log info "$LogPrefix: Empty hostname for '$leaseActIP', using generated host name '$hostname'"
  }
  :if ( [ :len $domain ] <= 0 ) do=\
  {
    :log warning "$LogPrefix: Empty domainname for '$leaseActIP', cannot create static DNS name"
    :error "Empty domainname for '$leaseActIP'"
  }

  :local fqdn ($hostname . "." .  $domain)

  /ip dns static
  :if ( [ :len [ find name=$fqdn and address=$leaseActIP and disabled=no ] ] = 0 ) do=\
  {
    add address=$leaseActIP name=$fqdn ttl=$ttl comment=$DHCPtag disabled=no
    :log info "$LogPrefix: Static domain name '$fqdn' created for '$leaseActIP' with ttl '$ttl'"
  }\
  else=\
  {
    :log warning "$LogPrefix: '$fqdn' already exists, cannot create static DNS name for '$leaseActIP'"
    :error "$LogPrefix: '$fqdn' already exists"
  }
}\
else=\
{
  # DHCP lease removed

  /ip dns static
  :local dnsDhcpId
  :set dnsDhcpId [ find address=$leaseActIP and comment=$DHCPtag ]
  :if ( [ :len $dnsDhcpId ] > 0 ) do=\
  {
    remove $dnsDhcpId
    :log info "$LogPrefix: Static DNS name(s) for '$leaseActIP' removed"
  }
}