'Get local IP Address from a known MAC Address in Python?
I am running a Python Script on the Raspberry Pi in order to get measured data out of a Smart Plug. In my script I need to write the IP-Address of the Smart Plug so that I can retrieve the data it was measured. The problem is that I need to be able to take the Smart Plug to different places without having to hard code its new local IP-Address every time.
I have the MAC Address so I am hoping there is an "easy" way to add a couple lines of code and retrieve the local IP-Address from the MAC (?) in the Python Script. Thanks!
Solution 1:[1]
What you're describing can be accomplished by crafting an ARP packet to get that info.
Generally something like:
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)
ip = pkt[ARP].psrc
Solution 2:[2]
This can be achieve using arp command in the subprocess module. Here is code. Checked in windows.
import subprocess
cmd = 'arp -a | findstr "ff-ff-ff-ff-ff-ff" '
returned_output = subprocess.check_output((cmd),shell=True,stderr=subprocess.STDOUT)
print(returned_output)
parse=str(returned_output).split(' ',1)
ip=parse[1].split(' ')
print(ip[1])
Solution 3:[3]
The local ip address is not based on the MAC address. The router uses DHCP to give the devises an ip address. So there is no way to tell the router which IP he must give you other than changing the settings.
I would rather try to broadcast the ip and on the raspberry listen on the broadcast channel for the message you are looking for.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | etskinner |
| Solution 2 | |
| Solution 3 | desmaxi |
