1. 需要按照scapy。
不同系统可能名字不同,在ubuntu上包名是python3-scapy。来自网络,精简到只有发现dhcp服务器功能。
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
"""
test_dhcp
"""
from scapy.all import (
Ether, IP, TCP, UDP, DNS, DNSQR, BOOTP, DHCP,
sr, sr1, srp, srp1,
srloop, srploop,
make_table,
wireshark,
get_if_raw_hwaddr,
sniff
)
def test_dhcp():
"""
from scapy/docs/usage
"""
from scapy.all import conf
conf.checkIPaddr =False
iface = conf.iface
fam, hw = get_if_raw_hwaddr(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"])
)
ans, unans = srp(dhcp_discover, multi=True, timeout=10, iface=iface)
for p in ans:
print("=", p[1][Ether].src, p[1][IP].src, "=")
def main():
test_dhcp()
if __name__ == "__main__":
main()