Code: Select all
#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)
}
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.