Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 15133

Scripting • Re: DNS script not normalizing characters of a particular host

$
0
0
Yay, I figured it out.

The script was only normalizing DHCP comments, and all of my other hosts had hostnames that did not need normalizing.

So, the solution was to add a single line:
Code:
  :set hostname [$normalizeHostname name=$hostname]
Added below:
Code:
  :set hostname [ get $leaseId host-name ]  :set comment [ get $leaseId comment ]  /
This meant that the hostname was normalized before being set:
Code:
  :set fqdn "$hostname.$domain"
Thus resulting in a normalised hostname used to add a static dns entry:
Code:
  /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
Here's the entire script:
Code:
:local DHCPtag:set DHCPtag "DHCP2DNS":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# Normalize hostname (e.g. "-= My Phone =-" -> "My-Phone")# - truncate length to 63 chars# - substitute disallowed chars with a hyphen# param: name:local normalizeHostname do={  :local result;  :local isInvalidChar true;  :for i from=0 to=([:len $name]-1) do={    :local char [:pick $name $i];    :if ($i < 63) do={      :if ($char~"[a-zA-Z0-9]") do={        :set result ($result . $char);        :set isInvalidChar false;      } else={        :if (!$isInvalidChar) do={          :set result ($result . "-");          :set isInvalidChar true;        };      };    };  };  # delete trailing hyphen  :if ($isInvalidChar) do={    :set result [:pick $result 0 ([:len $result]-1)];  }  :return $result;};  /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 ]  /  :set hostname [$normalizeHostname name=$hostname]  :if ( [ :len $hostname ] <= 0 ) do={ :set hostname [$normalizeHostname name=$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  }  /}

Statistics: Posted by steve1 — Fri Jan 26, 2024 4:07 am



Viewing all articles
Browse latest Browse all 15133

Trending Articles