使用python从MAC获取IP地址 [英] Obtain IP Address from MAC using python
问题描述
我的本地网络中有一台设备并且知道它的MAC地址。它通过DHCP自动获取IP地址。我想获得这个IP。
I have a device in my local network and know its MAC Address. It gets it's IP address automatically via DHCP. I want to obtain this IP.
我不想使用nmap但是scapy会好的,因为我是从一个kivy应用程序导入的。
I don't want to use nmap but scapy would be fine, as I'm importing from a kivy app.
我发现:
from scapy.all import srp, Ether, ARP
ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst="192.168.1.0/24"),timeout=2)
和
ans.summary()
我得到以下输出:
Ether / ARP who has 192.168.43.1 says 192.168.43.92 ==> Ether / ARP is at 3e:f8:d9:45:1b:3d says 192.168.43.1
我的两个问题是:
1:我必须用sudo python运行脚本,我不确定kivy中的权限。
1: I have to run the script with sudo python and I'm not sure about the permissons in kivy.
2:有没有一种简单的方法可以从.summary()获取IP / MAC的变量?
2: Is there a simple way to get the variables for IP/MAC out of the .summary()?
推荐答案
是的,你可以用scapy做到这一点。以下是ARP is-at数据包的示例:
Yes, you can do that using scapy. Here is an example of an ARP is-at packet:
###[ Ethernet ]###
dst= 08:00:27:fa:25:8e
src= 08:00:27:b1:af:68
type= 0x806
###[ ARP ]###
hwtype= 0x1
ptype= 0x800
hwlen= 6
plen= 4
op= is-at
hwsrc= 08:00:27:b1:af:68
psrc= 192.168.56.102
hwdst= 08:00:27:fa:25:8e
pdst= 192.168.56.101
###[ Padding ]###
load= '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00'
您可以指定hwsrc和psrc值到这样的变量
You can assign the hwsrc and psrc values to a variable like this
srcMAC = pkt[ARP].hwsrc
srcIP = pkt[ARP].psrc
或者只是像这样打印
print pkt[ARP].hwsrc
print pkt[ARP].psrc
希望这有帮助。
这篇关于使用python从MAC获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!