-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdhcp-dns.rsc
89 lines (77 loc) · 2.3 KB
/
dhcp-dns.rsc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Name of the DHCP server instance:
:local dhcpServer "dhcpname"
# DNS zone suffix:
:local dnsSuffix ".myzone.local"
# DNS TTL:
:local ttl "00:02:00"
# Enable console debug:
#:local debugout do={ :put ("DEBUG: " . [:tostr $1]); }
# Disable console debug:
:local debugout do={ :log debug $1; }
#----- END OF CONFIG -----#
:local cleanHostname do={
:local max ([:len $1] - 1);
:if ($1 ~ "^[a-zA-Z0-9]+[a-zA-Z0-9\\-]*[a-zA-Z0-9]+\$" && ([:pick $1 ($max)] != "\00")) do={
:return ($1);
} else={
:local cleaned "";
:for i from=0 to=$max do={
:local c [:pick $1 $i]
:if ($c ~ "^[a-zA-Z0-9]{1}\$") do={
:set cleaned ($cleaned . $c)
} else={
if ($c = "-" and $i > 0 and $i < $max) do={
:set cleaned ($cleaned . $c)
}
}
}
:return ($cleaned);
}
}
# Cache current DHCP lease IDs and cleaned hostnames
:local dhcpLeases
:set $dhcpLeases [:toarray ""]
/ip dhcp-server lease
:foreach lease in=[find where server=$dhcpServer] do={
:local hostRaw [get $lease host-name]
:if ([:len $hostRaw] > 0) do={
:local hostCleaned
:set hostCleaned [$cleanHostname $hostRaw]
:set ($dhcpLeases->$hostCleaned) $lease
}
}
# Remove or update stale DNS entries
/ip dns static
:foreach record in=[find where comment="<AUTO:DHCP:$dhcpServer>"] do={
:local fqdn [get $record name]
:local hostname [:pick $fqdn 0 ([:len $fqdn] - [:len $dnsSuffix])]
:local leaseMatch ($dhcpLeases->$hostname)
:if ([:len $leaseMatch] < 1) do={
$debugout ("Removing stale DNS record '$fqdn'")
remove $record
} else={
:local lease [/ip dhcp-server lease get $leaseMatch address]
:if ($lease != [get $record address]) do={
$debugout ("Updating stale DNS record '$fqdn' to $lease")
:do {
set $record address=$lease
} on-error={
:log warning "Unable to update stale DNS record '$fqdn'"
}
}
}
}
# Add new DNS entries
/ip dns static
:foreach k,v in=$dhcpLeases do={
:local fqdn ($k . $dnsSuffix)
:if ([:len [find where name=$fqdn]] < 1) do={
:local lease [/ip dhcp-server lease get $v address]
$debugout ("Creating DNS record '$fqdn': $lease")
:do {
add name=$fqdn address=$lease ttl=$ttl comment="<AUTO:DHCP:$dhcpServer>"
} on-error={
:log warning "Unable to create DNS record '$fqdn'"
}
}
}