Python Scapy ARP扫描:解决send_arp_reply()缺少参数错误
Python Scapy ARP扫描:解决'send_arp_reply()缺少参数'错误
在使用Python的Scapy库进行网络扫描时,你可能会遇到 'TypeError: send_arp_reply() missing 4 required positional arguments: 'src_ip', 'src_mac', 'dst_ip', and 'dst_mac'' 错误。这个错误提示表明 send_arp_reply() 函数需要四个参数:源IP地址、源MAC地址、目标IP地址和目标MAC地址。
错误分析
出现这个错误是因为你在调用 send_arp_reply() 函数时没有提供所有必需的参数。send_arp_reply() 函数用于构建和发送ARP响应数据包,它需要知道以下信息:
- src_ip: 发送ARP响应的设备的IP地址。
- src_mac: 发送ARP响应的设备的MAC地址。
- dst_ip: 接收ARP响应的设备的IP地址。
- dst_mac: 接收ARP响应的设备的MAC地址。
解决方案
要解决这个问题,你需要修改 send_arp_reply() 函数的定义,并在调用它时提供所有必需的参数。以下是如何修改代码的示例:
from scapy.all import *
def send_arp_reply(ifname, src_ip, src_mac, dst_ip, dst_mac):
# 发送ARP响应
arp_reply = ARP(op=2, hwsrc=src_mac, psrc=src_ip, hwdst=dst_mac, pdst=dst_ip)
ether = Ether(dst=dst_mac, src=src_mac)
packet = ether / arp_reply
sendp(packet, iface=ifname, verbose=False)
# 接收ARP响应
timeout = time.time() + 1
while True:
if time.time() > timeout:
break
response = sniff(iface=ifname, filter='arp and src host ' + dst_ip, count=1, timeout=1)
if len(response) > 0:
return {'src_ip': response[0][ARP].psrc, 'src_mac': response[0][ARP].hwsrc}
return None
def scan_network(ifname):
active_hosts = {}
for i in range(1, 255):
dst_ip = '192.168.197.' + str(i)
send_arp_request(ifname, '192.168.197.1', get_mac_address(ifname), dst_ip)
time.sleep(0.1)
# 调用send_arp_reply函数时,传入所有必需的参数
response = send_arp_reply(ifname, '192.168.197.1', get_mac_address(ifname), dst_ip, 'ff:ff:ff:ff:ff:ff')
if response is not None:
active_hosts[response['src_ip']] = response['src_mac']
return active_hosts
代码说明:
-
send_arp_reply()函数修改:- 函数定义现在包含所有四个必需的参数:
src_ip,src_mac,dst_ip,dst_mac。 - 函数内部使用这些参数构建ARP响应数据包。
- 函数定义现在包含所有四个必需的参数:
-
scan_network()函数调用:- 在调用
send_arp_reply()函数时,我们现在传入了所有四个必需的参数,包括源IP地址、源MAC地址、目标IP地址和广播MAC地址 ('ff:ff:ff:ff:ff:ff')。
- 在调用
通过这些修改,你应该能够解决 'TypeError: send_arp_reply() missing 4 required positional arguments' 错误,并成功使用Scapy进行网络扫描。
原文地址: https://www.cveoy.top/t/topic/jm8o 著作权归作者所有。请勿转载和采集!