dhcp-to-dns 2.48 KB
Newer Older
1
#!rsc
Christian Hesse's avatar
Christian Hesse committed
2
# RouterOS script: dhcp-to-dns
3
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
Christian Hesse's avatar
Christian Hesse committed
4 5 6
#
# check DHCP leases and add/remove/update DNS entries

7 8
:global CharacterReplace;

9 10 11
:global Identity;
:global Domain;
:global HostNameInZone;
Christian Hesse's avatar
Christian Hesse committed
12

13 14 15
:local Zone;
:if ($HostNameInZone = true) do={
  :set Zone ("dhcp." . $Identity . "." . $Domain);
Christian Hesse's avatar
Christian Hesse committed
16
} else={
17
  :set Zone ("dhcp." . $Domain);
Christian Hesse's avatar
Christian Hesse committed
18
}
19
:local Ttl 5m;
20
:local CommentPrefix "managed by dhcp-to-dns for ";
Christian Hesse's avatar
Christian Hesse committed
21

22 23 24 25 26
:foreach DnsRecord in=[ / ip dns static find where comment ~ $CommentPrefix ] do={
  :local DnsRecordVal [ / ip dns static get $DnsRecord ];
  :local MacAddress [ $CharacterReplace ($DnsRecordVal->"comment") $CommentPrefix "" ];
  :if ([ / ip dhcp-server lease print count-only where mac-address=$MacAddress address=($DnsRecordVal->"address") dynamic=yes ] > 0) do={
    :log debug ("Lease for " . $MacAddress . " (" . $DnsRecordVal->"host-name" . ") still exists. Not deleting DNS entry.");
Christian Hesse's avatar
Christian Hesse committed
27
  } else={
28
    :local Found false;
29 30
    :log info ("Lease expired for " . $MacAddress . " (" . $DnsRecordVal->"name" . "), deleting DNS entry.");
    / ip dns static remove $DnsRecord;
Christian Hesse's avatar
Christian Hesse committed
31 32 33
  }
}

34
:foreach Lease in=[ / ip dhcp-server lease find where dynamic=yes ] do={
35 36 37
  :local LeaseVal [ / ip dhcp-server lease get $Lease ];
  :local Comment ($CommentPrefix . $LeaseVal->"mac-address");
  :local HostName [ $CharacterReplace ($LeaseVal->"host-name") " " "" ];
38
  :if ($HostName = "") do={
39
    :set HostName [ $CharacterReplace ($LeaseVal->"mac-address") ":" "-" ];
40
  }
Christian Hesse's avatar
Christian Hesse committed
41

42
  :local Fqdn ($HostName . "." . $Zone);
43 44 45 46
  :local DnsRecord [ / ip dns static find where name=$Fqdn ];
  :if ([ :len $DnsRecord ] > 0) do={
    :local DnsIp [ / ip dns static get $DnsRecord address ];

47
    :local HostLeases [ / ip dhcp-server lease find where mac-address=($LeaseVal->"mac-address") dynamic=yes ];
48 49
    :if ([ :len $HostLeases ] > 1) do={
      :set ($LeaseVal->"address") [ / ip dhcp-server lease get ($HostLeases->([ :len $HostLeases ] - 1)) address ];
50
    }
Christian Hesse's avatar
Christian Hesse committed
51

52
    :if ($DnsIp = $LeaseVal->"address") do={
53
      :log debug ("DNS entry for " . $Fqdn . " does not need updating.");
Christian Hesse's avatar
Christian Hesse committed
54
    } else={
55 56
      :log info ("Replacing DNS entry for " . $Fqdn . ", new address is " . $LeaseVal->"address" . ".");
      / ip dns static set name=$Fqdn address=($LeaseVal->"address") ttl=$Ttl comment=$Comment $DnsRecord;
Christian Hesse's avatar
Christian Hesse committed
57
    }
58
  } else={
59 60
    :log info ("Adding new DNS entry for " . $Fqdn . ", address is " . $LeaseVal->"address" . ".");
    / ip dns static add name=$Fqdn address=($LeaseVal->"address") ttl=$Ttl comment=$Comment;
Christian Hesse's avatar
Christian Hesse committed
61 62
  }
}