python编写代码实现ARP的活跃主机发现,要求探测的目标主机实现固定和手工输入(单个或区间主机)和实现区间主机自动化ARP探测,共编写三种
时间: 2024-09-27 07:17:05 浏览: 90
在Python中,我们可以使用`socket`模块以及`arp`库来实现主动的ARP(地址解析协议)广播,以便发现目标主机的存在。以下是三种实现方式:
1. **固定IP手动探测**:
```python
import socket
import subprocess
from time import sleep
def arp_probe(ip):
arp_request = "\n".join(["who has %s" % ip, "tell %s" % ip, "quit"])
arp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_ARP)
arp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
while True:
try:
arp_socket.send(arp_request.encode())
response, addr = arp_socket.recvfrom(1024) # 捕获响应包
break # 如果收到响应,结束循环
except Exception as e:
print("Failed to send ARP request:", str(e))
sleep(1) # 等待一秒再尝试
# 示例
target_ip = input("Enter the fixed IP to probe: ")
arp_probe(target_ip)
阅读全文