Community discussions

 
wilsonlmh
newbie
Topic Author
Posts: 26
Joined: Fri Oct 10, 2014 9:44 pm

Sync DNS entries with DHCP leases

Mon Dec 29, 2014 3:53 pm

Hi everybody I wrote a script to sync DNS entries with DHCP leases so that you don't need to maintain two list manually. Actually it does store all the leases and hostnames in the script and then apply(replace) them to DNS server and DHCP server. So you only need to care the config in script(which archive centralization). All you need is below:
#All DHCP&DNS static configuration within same zone(described below) refer to here. This script will update(delete then re-create) all entries that match config below. So it's useless for changing anything in the DHCP Server and DNS tab. Read the guide here if any matter relate to static DNS/DHCP entries need to be changed.

#Environment configuration
:local dhcpserver "Internal Network"
:local zone “example.com”
:global hostListString



#This is to define how many list you have. The naming rule will be fixed as hostListStringX
:local ifrom 1;
:local ito 2;

#Hosts(machines/devices) Lists
#Follow format: $ip,$hostname,$mac_addr,$client_id
#Each entry in ONE single line!
#Content between commas cannot leave any spaces!
# $client_id is "1:" follow by lowercase of mac address. It can also be retrieved from DHCP Server->Lease.
#Note: :global is required because I use :parse below
#Bug: The last line must be a empty line.
#Bug: The size of a variable couldn't be larger then 4096bytes. This is a hard-code issue from mikrotik. So maybe you need to create more than 1 list. Or for somebody(like me), I use different list to separate different subnets. Anyway, all up to you.

#List below
:global hostListString1 "192.168.1.11,server1,00:DD:CC:EE:EE:11,1:0:dd:cc:ee:ee:11
192.168.1.12,server2,00:DD:CC:EE:EE:12,1:0:dd:cc:ee:ee:12
192.168.1.13,server3,00:DD:CC:EE:EE:13,1:0:dd:cc:ee:ee:13
";

:global hostListString2 "192.168.2.11,client1,00:DD:CC:EE:EE:21,1:0:dd:cc:ee:ee:21
192.168.2.12,client2,00:DD:CC:EE:EE:22,1:0:dd:cc:ee:ee:22
192.168.2.13,client3,00:DD:CC:EE:EE:23,1:0:dd:cc:ee:ee:23
";

#Script begin
#Deleting old DNS entries

/ip dns static remove [find where name ~ (".*\\.".$zone)]

:for i from=$ifrom to=$ito do={
:local changeList [:parse (":global hostListString". [:tostr $i] . "; :global hostListString; :set \$hostListString \$hostListString" . [:tostr $i])];
$changeList;
:local contentLen [ :len $hostListString ] ;
:local lineEnd 0;
:local line "";
:local lastEnd 0;
:local hostArray;


	:do {
		:set lineEnd [:find $hostListString "\n" $lastEnd ];
		:set line [:pick $hostListString $lastEnd $lineEnd] ;
		:set lastEnd ( $lineEnd + 1 ) ;

		:set hostArray [:toarray $line];

		:if ([:len $line] >= 7 ) do={

			#Add back DNS entries
			/ip dns static add address=[:pick $hostArray 0] name=([:pick $hostArray 1].".".$zone)
			#Search if the host is presenting in DHCP server
			/ip dhcp-server lease
			:local searchingDHCP [find where mac-address=[:pick $hostArray 2]];
			:if ($searchingDHCP != "") do={
				#Host present! Run make-static(rather then delete and re-create)
				#This two command will generate a warning, so catch the exception.
				:do { make-static number=$searchingDHCP } on-error={};
				:do { set number=$searchingDHCP address=[:pick $hostArray 0] server=$dhcpserver comment="-)createdBy=DHCP2DNS" } on-error={};
			} else={
				#Host not present! Delete if exist and re-create
				:if ([find where mac-address=[:pick $hostArray 2]] != "") do={ remove number=(find where mac-address=[:pick $hostArray 2]) }

				add address=[:pick $hostArray 0] mac-address=[:pick $hostArray 2] client-id=[:pick $hostArray 3] server=$dhcpserver  comment="-)createdBy=DHCP2DNS"
			}

              
	       } else={ :put "NULL entry"; }

	} while ($lineEnd < $contentLen-1)
}
Just follow the format to put you MAC address,IP,hostname inside the script. And then create a schedule task to refresh any updates(may you don't need).
Caution: Please backup all your config and read the remarks before you apply any script. This script may clear all your DHCP and DNS entries if your config is invalid.
Last edited by wilsonlmh on Mon Dec 29, 2014 4:41 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Sync DNS entries with DHCP leases

Mon Dec 29, 2014 4:17 pm

Wouldn't it be more efficient and less error prone to use a lease-script on the DHCP server? And in it, you just compute the desired name, prepending it to the zone.

Speaking of which... What name is this generating? I can see it appends the zone name in the end, but what's the previous part based on?
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
wilsonlmh
newbie
Topic Author
Posts: 26
Joined: Fri Oct 10, 2014 9:44 pm

Re: Sync DNS entries with DHCP leases

Mon Dec 29, 2014 4:52 pm

Wouldn't it be more efficient and less error prone to use a lease-script on the DHCP server? And in it, you just compute the desired name, prepending it to the zone.

Speaking of which... What name is this generating? I can see it appends the zone name in the end, but what's the previous part based on?
For the first question, that is another problem I concerned. For some device with static ip but within same dns suffix, lease-script is not a good idea(but works anyway, you just need to create a lot of null lease). For a concept of management, mikrotik should have a centralized way to config both DNS and DHCP(and even RADIUS). Otherwise, (in case of commercial usage) it will be replace by some directory service(e.g. AD). So this is just a starting point for that concept. I think you will love it after more and more people contribute.

For the second question, the string that will put into DNS server is the FQDN, which is "$hostname.$zone", just like server1.example.com


Further talking, I can actually ignore the MAC address(when it's null in the list) and only add the DNS entry. So you don't need to enter mac addresses of static IP devices. I will add this feature soon.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Sync DNS entries with DHCP leases

Mon Dec 29, 2014 5:18 pm

For some device with static ip but within same dns suffix, lease-script is not a good idea(but works anyway, you just need to create a lot of null lease).
If a lease/IP is static, why would you need to schedule it in the first place? All you need is a single script to maybe add the static entry and then add the DNS entry. It won't be changing until you explicitly change it SOMEWHERE manually anyway.
mikrotik should have a centralized way to config both DNS and DHCP(and even RADIUS). Otherwise, (in case of commercial usage) it will be replace by some directory service(e.g. AD). So this is just a starting point for that concept. I think you will love it after more and more people contribute.
I could probably whip out a simple API application for that, if you can tell me more fully the features this needs, and how they're done "manually". It should be nicer to work with than a script, although admittedly, it means you need an additional device to run the management application, making this an equally "hacky" solution.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
jkarras
Member Candidate
Member Candidate
Posts: 224
Joined: Fri Sep 06, 2013 3:07 am
Location: Utah, USA

Re: Sync DNS entries with DHCP leases

Tue Dec 30, 2014 6:59 am

I created a script to dynamically add and remove DNS entries via the lease-script. It also tracks which lease created the entry so two machines with the same name don't have conflicts. First machine to claim the name gets it.

https://github.com/karrots/ROS-DDNS
 
midenok
newbie
Posts: 39
Joined: Fri Dec 27, 2013 5:34 pm

Re: Sync DNS entries with DHCP leases

Fri Dec 07, 2018 9:26 pm

RouterOS 6.34
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1303
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Sync DNS entries with DHCP leases

Sat Dec 08, 2018 12:12 am

An old implementation from 2015. May work, have not tested it.
But this command: "/interface wireless" would fail on routers with out wifi.
Should be an on-error option.

PS if you are running RouterOS 6.34, as your tag line says, you should upgrade now.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
eworm
Member
Member
Posts: 393
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Sync DNS entries with DHCP leases

Wed Dec 12, 2018 10:23 pm

I have another one:
dhcp-to-dns on github or cgit.

(This depends on other scripts from the same repository, see README to setup.)
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1303
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Sync DNS entries with DHCP leases

Thu Dec 13, 2018 1:58 pm

Looks interesting, but have some question.
The readme file I found only describe how the script update process.
Do the DHCP script runs at the DHCP or scheduled?
What if you have set a DNS name for a host manual, do it get overwritten?
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
eworm
Member
Member
Posts: 393
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Sync DNS entries with DHCP leases

Thu Dec 13, 2018 3:16 pm

Looks interesting, but have some question.
The readme file I found only describe how the script update process.
Do the DHCP script runs at the DHCP or scheduled?
What if you have set a DNS name for a host manual, do it get overwritten?
Some scripts need extra documentation... Will look into that when I find some spare time. :roll:

For dhcp-to-dns I do both, run it from dhcp-server as lease script (to add new records) and from scheduler to remove obsolete records. For a dhcp client with name "host", mikrotik device with name "router" and domain set to "example.com" the DNS records looks like "host.dhcp.router.example.com" (or "host.dhcp.example.com" if you set $"hostname-in-zone" in global-config to false). Unless you use that for your manual records nothing is overwritten.
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts

Who is online

Users browsing this forum: No registered users and 9 guests