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
Code: Select all
# 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";
}
}
}