如何查询特定的 DHCP 服务器

如何查询特定的 DHCP 服务器

有没有办法查询特定 DHCP 服务器的 IP 地址?类似于

dig @_IP_ADDRESS_ _server_name
dig @8.8.8.8 apple.com

谢谢

答案1

只要你与 DHCP 服务器位于同一子网,你就可以使用 scapy发送 DHCP 请求并获取响应

例如,我将其改编为以下内容,显示网络上的所有 DHCP 服务器以及它们所服务的范围:

[michael:~/prog/util]$ sudo ./findDhcpServers.py 
Begin emission:
Finished to send 1 packets.
*................................
Received 33 packets, got 1 answers, remaining 0 packets
DHCP offers received:
MAC: 00:1b:64:33:df:29, Server IP: 192.168.0.6, Offer IP: 192.168.0.135
    Mask: 255.255.255.0, Router: 192.168.0.1, Domain: office.myworkplace.ca

其中一种方法可能就是您想要的。


代碼:

#!/usr/bin/python
# Michael Brown <[email protected]>

# idea stolen from http://bb.secdev.org/scapy/wiki/doc/IdentifyingRogueDHCPServers

from __future__ import print_function
from scapy.all import *

import sys

# Turn off response IP address validation
conf.checkIPaddr = False

# Set up the interface
fam,hw = get_if_raw_hwaddr(conf.iface)

dhcp_discover = Ether(dst="ff:ff:ff:ff:ff:ff")/IP(src="0.0.0.0",dst="255.255.255.255")/UDP(sport=68,dport=67)/BOOTP(chaddr=hw)/DHCP(options=[("message-type","discover"),"end"])

#print("Press Ctrl-C after several seconds...", fd=sys.stderr)
ans, unans = srp(dhcp_discover, multi=True, timeout=5)

if len(ans) == 0:
    print("No DHCP offers received", file=sys.stderr)
else:
    print("DHCP offers received:")
    for pair in ans:
        p = pair[1]
        d = p[DHCP]
        print("MAC: {0}, Server IP: {1}, Offer IP: {2}\n    Mask: {3}, Router: {4}, Domain: {5}".format(
            p[Ether].src,
            p[IP].src,
            p[BOOTP].yiaddr,
            filter(lambda x: x[0] == 'subnet_mask', d.options)[0][1],
            filter(lambda x: x[0] == 'router', d.options)[0][1],
            filter(lambda x: x[0] == 'domain', d.options)[0][1],
            ))

答案2

捕获 DHCP 交换的最简单方法是在配置过程中在 DHCP 服务器或客户端上运行数据包捕获(在ipconfig /release /renewWindows 和dhclient -r; dhclient*nix 上可以轻松完成)。我强烈建议使用 Wireshark 来实现这一点,或者在命令行实用程序中捕获交换(tcpdump例如在 *nix 中),然后在 Wireshark 中打开捕获以轻松分析交换。

答案3

dhclient(至少在某些系统上)有一个 -s 选项来指定特定的服务器,而不是使用默认广播。

相关内容