Community discussions

MikroTik App
 
cgallery
newbie
Topic Author
Posts: 46
Joined: Tue Apr 24, 2018 5:25 am

Need help modifying a DHCP2DNS script

Thu Jan 18, 2024 4:24 pm

I'm using this script to provide DHCP2DNS. I have some HP printers for which it doesn't work properly, something in their DHCP client doesn't provide a hostname.

On some HP models, a setting ("DHCPv4 FQDN RFC 4702 Compliance") I was able to disable fixed the hostname issue, but on some very new ones, this doesn't seem to help.

So I set a DNS manually.

BUT, if the user restarts the device, the script I found (I found it at this site) unregisters the static DNS I've added.

So I would like to modify the script to check the TTL value of the DNS entry, and it it is 1d, I'd like it to not delete the DNS entry.

Here is the script I am using, can anyone help me?

I believe that last condition (" :if ( [ :len $dnsDhcpId ] > 0 ) do=\") needs to be modified but I'm not familiar with this programming language.

*****
:local DHCPtag

: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

  /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 ]
  /

  :if ( [ :len $hostname ] <= 0 ) do={ :set hostname $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 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 ]

 :if ( [ :len $dnsDhcpId ] > 0 ) do=\
  {
    :log info "DHCP2DNS: removing static domain name(s) for address $leaseActIP"
    remove $dnsDhcpId
  }
  /
}
 
cgallery
newbie
Topic Author
Posts: 46
Joined: Tue Apr 24, 2018 5:25 am

Re: Need help modifying a DHCP2DNS script

Fri Jan 19, 2024 5:34 pm

No replies yet, so I'm trying to figure this out myself.

I've added a local variable dnsComment. Once the script has the dnsDhcpid, I try to use that to derive the comment set in the DNS entry. If there is no comment (len <= 0), I allow the script to remove the entry. if there IS a comment, I do NOT allow the script to delete the entry.

I'm going this route as I've never seen the script actually set the comment field. So I figured if I need to set a DNS entry manually, I could just add a comment like "Do not remove this!" and the script will then leave it alone..

Comments? I don't know how to debug something like this, if I try it.

This is the new bottom of the script, where DNS entries are removed:

{
  /ip dns static
  :local dnsDhcpId
  :local dnsComment
  :set dnsDhcpId [ find address=$leaseActIP ]
  :set dnsComment [ get $dnsDhcpID comment ]

  :if ( [ :len $dnsDhcpId ] > 0 and [ :len $dnsComment ] <= 0) do=\
  {
    :log info "DHCP2DNS: removing static domain name(s) for address $leaseActIP"
    remove $dnsDhcpId
  }
  /
}
 
cgallery
newbie
Topic Author
Posts: 46
Joined: Tue Apr 24, 2018 5:25 am

Re: Need help modifying a DHCP2DNS script

Fri Jan 19, 2024 8:13 pm

Okay I got it to work, here is the new script bottom.

You can see some debugging lines that have been commented-out. That was the only way I could figure to test the code while I was working on it.

Anyway, here:
else=\
{
  /ip dns static
  # :log error "DHCP2DNS: I'm getting ready for the removal"
  :local dnsDhcpId
  :local dnsComment
  :set dnsDhcpId [ find address=$leaseActIP ]
  # :log error "DHCP2DNS: The IP in question is $leaseActIP"
  :set dnsComment [ get [ find address=$leaseActIP ] comment ]
  # :log error "DHCP2DNS: The comment is $dnsComment"

  :if ( [ :len $dnsDhcpId ] > 0 and [ :len $dnsComment ] <= 0) do=\
  {
    :log info "DHCP2DNS: removing static domain name(s) for $leaseActIP"
    remove $dnsDhcpId
  } else=\
  {
    :log error "DHCP2DNS: not removing static domain names(s) for $leaseActIP due to comment"
  }
  /
}

Who is online

Users browsing this forum: No registered users and 3 guests