Community discussions

MikroTik App
 
woro
Member Candidate
Member Candidate
Topic Author
Posts: 104
Joined: Sun May 24, 2015 12:47 am

dns update script

Mon Nov 21, 2016 9:54 pm

Hi,
I'm trying to get DNS update script running which adds DNS entries for DHCP leases.
As a start I've used http://wiki.mikrotik.com/wiki/Setting_s ... DHCP_lease
I'm trying to use the last example but try to change it in a way that it does not remove entries which were (based on the TTL setting) not added by the script itself.
:local zone "myzone.org";
:local ttl "00:05:00"
:local hostname
:local ip
:local dnsip
:local dhcpip
:local dnsnode
:local dhcpnode

/ip dns static;
:foreach i in=[find where name ~ (".*\\.".$zone) ] do={
:set hostname [ get $i name ];
:set hostname [ :pick $hostname 0 ( [ :len $hostname ] - ( [ :len $zone ] + 1 ) ) ];
/ip dhcp-server lease;
:set dhcpnode [ find where host-name=$hostname ];
:if ( [ :len $dhcpnode ] > 0) do={
:log debug ("Lease for ".$hostname." still exists. Not deleting.");
} else={
# there's no lease by that name. Maybe this mac has a static name.
:local found true;
:log warning (get $i ttl);
:log warning (get $i name);
:log warning ($hostname);
:if ([get $i ttl] = $ttl) do={
:set found false;
}
:log warning ("still alive");
:if ( $found ) do={
:log info ("Hostname ".$hostname." is static");
} else={
:log info ("Lease expired for ".$hostname.", deleting DNS entry.");
/ip dns static remove $i;
}
}
}

/ip dhcp-server lease;
:foreach i in=[find] do={
:set hostname ""
:local mac
:set dhcpip [ get $i address ];
:set mac [ get $i mac-address ];
:while ($mac ~ ":") do={
:local pos [ :find $mac ":" ];
:set mac ( [ :pick $mac 0 $pos ] . [ :pick $mac ($pos + 1) 999 ] );
};
:foreach n in=[ /system script environment find where name=("shost" . $mac) ] do={
:set hostname [ /system script environment get $n value ];
}
:if ( [ :len $hostname ] = 0) do={
:set hostname [ get $i host-name ];
}
:if ( [ :len $hostname ] > 0) do={
:set hostname ( $hostname . "." . $zone );
/ip dns static;
:set dnsnode [ find where name=$hostname ];
:if ( [ :len $dnsnode ] > 0 ) do={
# it exists. Is its IP the same?
:set dnsip [ get $dnsnode address ];
:if ( $dnsip = $dhcpip ) do={
:log debug ("DNS entry for " . $hostname . " does not need updating.");
} else={
:log info ("Replacing DNS entry for " . $hostname);
/ip dns static remove $dnsnode;
/ip dns static add name=$hostname address=$dhcpip ttl=$ttl;
}
} else={
# it doesn't exist. Add it
:log info ("Adding new DNS entry for " . $hostname);
/ip dns static add name=$hostname address=$dhcpip ttl=$ttl;
}
}
}
Now what I found is that the script apparently stops executing inbetween this:
:log warning (get $i ttl);
:log warning (get $i name);
:log warning ($hostname);
:if ([get $i ttl] = $ttl) do={
:set found false;
}
:log warning ("still alive");
It's also unclear to me why (get $i ttl) seems to work quite fine and (get $i name) is an empty string but as long has $hostname is still valid I do not care really.
The "still alive" output does not appear anymore though. Therefore I guess the if-statement might have some error but it was used in another example in a similar way.

Anyway I'm still quite new to ROS scripting and do not even have an idea how to debug this in a nice way.

Can someone help me what I did wrong?

Who is online

Users browsing this forum: No registered users and 41 guests