【问题标题】:Get DNS infos for local machine interfaces获取本地机器接口的 DNS 信息
【发布时间】:2021-11-26 20:47:34
【问题描述】:

我需要我 PC 上所有本地接口的 DNS 后缀。

有没有办法通过 Go 实现这一点?

  • 任何操作系统都是最好的情况
  • 必要:在 Windows 上工作

我已经尝试了net.Inferfaces() 和所有的 net 命令,但我没有找到任何关于 DNS 服务器的信息。

编辑

我找到了solution for the Windows-specific version,但如果有任何适用于 Linux 和 macOS 的东西会很有趣。

【问题讨论】:

    标签: go dns


    【解决方案1】:

    我认为没有适用于任何操作系统的解决方案。在 Linux 中,DNS 后缀不是特定于接口的,而是系统范围的,它在/etc/resolv.conf 中配置。以下是手册页的摘录:

    search Search list for host-name lookup.
                  By default, the search list contains one entry, the local domain name.  It is determined from the local hostname returned by gethostname(2); the local domain name is taken to be everything after the first '.'.  Finally, if the hostname does not contain a '.', the root domain is  assumed  as  the
                  local domain name.
    
                  This  may  be  changed  by listing the desired domain search path following the search keyword with spaces or tabs separating the names.  Resolver queries having fewer than ndots dots (default is 1) in them will be attempted using each component of the search path in turn until a match is found.
                  For environments with multiple subdomains please read options ndots:n below to avoid man-in-the-middle attacks and unnecessary traffic for the root-dns-servers.  Note that this process may be slow and will generate a lot of network traffic if the servers for the listed domains are not local, and
                  that queries will time out if no server is available for one of the domains.
    
                  If there are multiple search directives, only the search list from the last instance is used.
    

    net 包标准库parses this file 获取 DNS 配置,因此 DNS 解析器应该按预期运行,但是解析功能没有公开。

    libnetwork 库中的libnetwork.GetSearchDomains 函数应该可以帮助您。如果/etc/resolv.conf 中没有search 条目,则应使用主机名,可以通过os.Hostname func 获取。


    我相信这也适用于 FreeBSD 和 Mac OS,因为它们都是“类 UNIX”。但我不是 100% 确定。

    【讨论】:

    • 但基本上我仍然需要为 Windows 和 Unix 编写 2 个单独的版本,对吧?或者如果你知道我的意思的话,有什么方法可以让 1 个程序带有 2 个“树”
    • 是的,您可以使用buildconstraints 根据您正在编译的操作系统编译正确的版本。或使用runtime constants 进行动态检查(较慢)。这也是标准库处理操作系统之间差异的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多