dhcpd 重写 ddns 的无效客户端主机名

dhcpd 重写 ddns 的无效客户端主机名

我使用 isc-dhcpd 和 bind9 来执行 ddns。

它基本上可以正常工作,但来自具有无效名称的客户端的更新(例如“leif_opo5”或“Danfoss Connect CC”)会被拒绝。

我将手机的主机名更改为 leif-opo5,并且已注册,但我无法更改丹佛斯设备的硬编码主机名。

我可以对名称进行硬编码并赋予其固定 IP,但可能会出现其他设备,所以我想知道:

isc-dhcpd 是否可以使用“坏字符”重写主机名,例如用‘-’替换?

答案1

@Lenne,我在回复你的原帖后立即解决了这个问题,但我忘了回头提供解决方案。因此,这个问题已经不在我的记忆中了,所以如果有任何错误,或者我忘记了任何细节,请告诉我。

使用Sidvind 的“Dhcpd on Commit”教程作为参考,我在 中添加了以下内容/etc/dhcp/dhcpd.conf,这会导致没有主机名(或具有无效主机名)的客户端被分配类似于 的内容ip-10-12-34-56

on commit {
    set clientIpHyphenated = binary-to-ascii(10, 8, "-", leased-address);

    # Check whether the client supplied a hostname with illegal characters,
    # e.g., "iHome SmartPlug-27C139" with a space or underscore in it.
    # This resolves failures when trying to update DNS forward maps:
    #   dhcpd: Unable to add forward map from
    #       iHome SmartPlug-27C139.JonathansSecretDomain.com. to 10.12.34.56: REFUSED
    if (option host-name ~~ "^[a-z0-9][a-z0-9\-]+[a-z0-9]$") {
        set valid-hostname-else-null = option host-name;
    } else {
        set valid-hostname-else-null = null;
    }

    # Get the client name from the first of the following:
    #   1. Client DHCP Option FQDN
    #   2. Client DHCP Option Hostname (and it is a valid hostname without spaces, underscores, etc)
    #   3. Name of static lease (host-decl-name)
    #   4. A generated name that looks like: "ip-10-12-34-56"
    #   5. "none", if all else fails. Shouldn't ever occur.
    set clientName = pick-first-value(
        option fqdn.hostname,
        valid-hostname-else-null,
        # Optional:
        # This can cause an annoying repetitive error in syslog and dhcpd.log that I suppressed:
        #   dhcpd[965395]: data: host_decl_name: not available
        # If you uncomment it, then also add my rsyslog code below.
        #host-decl-name,
        concat("ip-", clientIpHyphenated),
        "none"
    );

    # Set the dynamic hostname, which otherwise wouldn't have been set if the client didn't
    # request one. We also ensure above that it doesn't contain invalid characters, and that a
    # fallback hostname will be generated if needed.
    ddns-hostname = clientName;

    # Optional:
    # If the client supplied a hostname with illegal characters, log what we changed it to for ddns.
    # (e.g., "iHome SmartPlug-27C139" with a space or underscore in it.)
    #if (not (option host-name = valid-hostname-else-null)) {
    #    log(info, concat("Invalid hostname: \"", option host-name, "\" --> \"", clientName, "\""));
    #} elsif (not (option host-name = clientName)) {
    #    log(info, concat("Hostname overridden: \"", option host-name, "\" --> \"", clientName, "\""));
    #}
}

如果取消注释上述配置中的某些可选内容,则创建/etc/rsyslog.d/10-dhcpd.conf并添加以下内容:

# Log most dhcpd messages to /var/log/dhcpd.log.
if $programname == 'dhcpd' then {

    # Spam: If it's a notice message containing "...host_decl_name...", then don't log it.
    # I think this "stop" rule is specific enough to not be exploitable, but I'm open to feedback.
    if $msg contains "data: host_decl_name: not available" and $syslogseverity-text == 'error'
        then stop

    # Otherwise, log the message.
    /var/log/dhcpd.log

    # Prevent info and debug-level events from also appearing in the syslog.
    # (Stop will prevent any additional rules from being applied to the message, including
    # preventing it from being duplicated to the syslog later on.)
    # Higher severity messages will otherwise intentionally be allowed
    # to continue to be processed, and thus will be duped to syslog.
    # If you don't like this behavior, then just add an unconditional "stop".
    if $syslogseverity-text == 'info' or $syslogseverity-text == 'debug'
        then stop
}

最后运行:

sudo systemctl restart rsyslog.service isc-dhcp-server.service

虽然这个解决方案不能取代特定的无效字符(这似乎不可能?),它代替无效的主机名包含无效字符使用通用主机名,这足以满足我的需求,希望也适合您的需求。(我喜欢它还根据没有主机名的客户端的 IP 为其分配通用主机名。)

答案2

对于每个主机名无效的主机,您可以在其自己的“主机”声明中创建“主机名”规范:.. # 彩色墨水打印机扫描仪主机 EPSON1EE63C { 硬件以太网 a4:ee:57:1e:e6:3c; 主机名 EpsonPrt; } .. 注意 EPSON1EE63C 对于 dhcp 守护进程和 DDNS(如果使用)没有任何意义。它仅表示其中一个“主机”声明的原始标识符,但块内的“主机名”却表示原始标识符。

答案3

您能在 DHCP 配置的 DDNS 部分尝试以下内容吗?

ddns-updates on;
# DNS host name rewrite policy:
#   Name: Default
#   Valid characters: a-z0-9
#   Replace invalid characters by: -

相关内容