Dnsmasq等软件有一个功能:把局域网主机名作为DNS条目解析。这样我们就可以方便地用主机名访问局域网主机,而无需查询动态IP。RouterOS上的DNS和DHCP是两个组件,官方没有实现这一功能。以下为实现方法。
2016-11-21更新:修复了某些系统(如Synology DSM)在主机名后有一个空字符导致的兼容性问题。
进入IP/DHCP Server,在需要这一功能的DHCP Server的Lease Script中填入以下脚本,确定,然后重启系统即可。
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 |
# Domain to be added to your DHCP-clients hostname :local topdomain; # Domain suffix (without a "." before it) :set topdomain "local"; # Use ttl to distinguish dynamic added DNS records :local ttl; :set ttl "00:59:59"; # Set variables to use :local hostname; :local hostnamestripped; :local hostip; :local free; # Remove all dynamic records /ip dns static; :foreach a in=[find] do={ :if ([get $a ttl] = $ttl) do={ :put ("Removing: " . [get $a name] . " : " . [get $a address]); remove $a; } } /ip dhcp-server lease ; :foreach i in=[find] do={ /ip dhcp-server lease ; :if ([:len [get $i host-name]] > 0) do={ :set free "true"; :set hostname ([get $i host-name] . "." . $topdomain); :set hostnamestripped ""; :set hostip [get $i address]; # strip null character :for i from=0 to=([:len $hostname]) do={ :local char [:pick $hostname $i] :if ($char = "\00") do={ :set $char "" } :set hostnamestripped ($hostnamestripped . $char) } /ip dns static ; # Check if entry already exist :foreach di in [find] do={ :if ([get $di name] = $hostnamestripped) do={ :set free "false"; :put ("Not adding already existing entry: " . $hostnamestripped); } } :if ($free = true) do={ :put ("Adding: " . $hostnamestripped . " : " . $hostip ) ; /ip dns static add name=$hostnamestripped address=$hostip ttl=$ttl; } } } |