Python 从网络获取 ips 和 macs
Python getting ips and macs from network
我正在尝试从网络中获取所有 IP 及其关联的 MAC 地址。
到目前为止,我有以下代码:
eth = Ether(dst = "ff:ff:ff:ff:ff:ff")
arp = ARP(pdst = '198.13.13.1')
answered = srp1(eth / arp)
print answered[1].hwsrc
print answered[1].psrc
但这只会让我得到我输入的 Ip 的 MAC。
我想我必须使用:
answered, unanswered = srp(eth/arp)
并用子网地址修改 pdst,但我不知道该怎么做。
有什么想法吗?
您只需在 pdst
字段中输入您的网络。你想使用 timeout
因为有些探测可能得不到答案。例如:
ans, unans = srp(Ether(dst=ETHER_BROADCAST) / ARP(pdst="198.13.13.0/24"), timeout=1)
然后你需要解析ans
。如果你想获得关联的 MAC 和 IP 地址,你可以创建一个元组列表。例如:
res = [(pr.psrc, pr.hwsrc) for _, pr in ans]
print res
您还可以使用 arping()
,一个专门用于该目的的函数:
ans, unans = arping("198.13.13.0/24")
ans.show()
我正在尝试从网络中获取所有 IP 及其关联的 MAC 地址。
到目前为止,我有以下代码:
eth = Ether(dst = "ff:ff:ff:ff:ff:ff")
arp = ARP(pdst = '198.13.13.1')
answered = srp1(eth / arp)
print answered[1].hwsrc
print answered[1].psrc
但这只会让我得到我输入的 Ip 的 MAC。
我想我必须使用:
answered, unanswered = srp(eth/arp)
并用子网地址修改 pdst,但我不知道该怎么做。
有什么想法吗?
您只需在 pdst
字段中输入您的网络。你想使用 timeout
因为有些探测可能得不到答案。例如:
ans, unans = srp(Ether(dst=ETHER_BROADCAST) / ARP(pdst="198.13.13.0/24"), timeout=1)
然后你需要解析ans
。如果你想获得关联的 MAC 和 IP 地址,你可以创建一个元组列表。例如:
res = [(pr.psrc, pr.hwsrc) for _, pr in ans]
print res
您还可以使用 arping()
,一个专门用于该目的的函数:
ans, unans = arping("198.13.13.0/24")
ans.show()