Community discussions

MikroTik App
 
User avatar
dang21000
Member Candidate
Member Candidate
Topic Author
Posts: 120
Joined: Sat Feb 25, 2023 2:30 pm
Location: France

DHCPv4 server, script run failed randomly

Wed Aug 14, 2024 11:08 pm

Hi,

I've create the following script used to populate DNS server with DHCPv4 leases, it's very simple and work well.
It simple remove all previous DNS entries (identified by a comment) and for each leases, create one type A with ipaddr.

DHCPv4 server instance is configured with this command in the script field : /system/script/run dhcp_dns_guest1

Behaviors :
* when run manually, everything work fine and DNS entries are removed and created, like expected
* when run by DHCPv4 server, sometime it run, sometimes i get in system debug logs : executing script from dhcp failed, please check it manually

Observations :
* This script is very old and work since 7.3 maybe before
* I don't remember if i've see theses related errors in 7.14 or 7.15.

Question is : why sometimes it work and sometime no ?


Code of script dhcp_dns_guest1 :
Permissions : READ + WRITE
# dhcp server instance
:local dhcpinstance "dhcp_guest1";
:local topdomain "guest70.ginhoux.net";


# remove all dhcp instance dns static records
/ip dns static;
:foreach a in=[find] do={
  :local dnsentrytype [get $a type];
  :local dnsentrycomment [get $a comment];
  :local dnsentryhostname [get $a name];
  :local dnsentryip [get $a address];
  :if ($dnsentrytype != "FWD") do={
    :if ($dnsentrycomment = $dhcpinstance) do={
      :put ("$dhcpinstance: remove dns static entry $dnsentryhostname with ip $dnsentryip");
      :log info "$dhcpinstance: remove dns static entry $dnsentryhostname with ip $dnsentryip";
      remove $a;
    }
  }
}


# generate dhcp instance dns static records
:local leasehostname;
:local leaseip;
:local leasefree;
:local dnsentryttl "30d 00:00:00";
/ip dhcp-server lease;
:foreach i in=[find] do={
  # /ip dhcp-server lease ;
  :if ([:len [get $i host-name]] > 0) do={
    :if ([get $i server] = $dhcpinstance) do={
      :set leasefree "true";
      :set leasehostname ([get $i host-name] . "." . $topdomain);
      :set leaseip [get $i address];
      /ip dns static add name=$leasehostname comment=$dhcpinstance address=$leaseip ttl=$dnsentryttl;
      :put (info "$dhcpinstance: add dns static entry $leasehostname with ip $leaseip");
      :log info "$dhcpinstance: add dns static entry $leasehostname with ip $leaseip";
    }
  }
}
 
morf
Member Candidate
Member Candidate
Posts: 183
Joined: Tue Jun 21, 2011 5:31 pm
Location: Saint-Petersburg

Re: DHCPv4 server, script run failed randomly

Fri Sep 06, 2024 12:12 am

I have exactly the same behavior on version 7.15.3.