python-nmap如何获取dhcp_使用python从MAC获取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.

I don't want to use nmap but scapy would be fine, as I'm importing from a kivy app.

I found:

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)

and with

ans.summary()

I get the following output:

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

My two problems are:

1: I have to run the script with sudo python and I'm not sure about the permissons in kivy.

2: Is there a simple way to get the variables for IP/MAC out of the .summary()?

解决方案

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'

You can assign the hwsrc and psrc values to a variable like this

srcMAC = pkt[ARP].hwsrc

srcIP = pkt[ARP].psrc

or just print them like this

print pkt[ARP].hwsrc

print pkt[ARP].psrc

Hope that helps.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值