6

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!

5
  • You could put your NIC in promiscuous mode and sniff ARP packets. But if you're on the other side of a switch from the device you may never see those packets anyway. In general, ARP works to resolve IP addresses to MAC addresses, not the other way around. Can the device be made to broadcast something about itself when it connects to a network? Commented May 7, 2019 at 11:56
  • @DanielPryden Hi and thanks for your reply. I am pretty new to this so to be honest I don't really understand what you mean by "broadcast" something. Can you try again? Commented May 7, 2019 at 12:10
  • I have no idea what kind of "Smart Plug" this is (I assume some kind of power outlet control device?), so I have no idea what's possible, which is why I'm not writing an answer. The gist of my comment is that it's almost certainly impossible for your Raspberry Pi to determine the IP address of another device, even if it knows the MAC address of that device's physical adapter, unless that other device is transmitting packets to the network itself. The normal solution is for the device to be configured to send broadcast UDP packets of some kind until someone notices it and starts communicating. Commented May 7, 2019 at 12:19
  • Perhaps this stackoverflow.com/questions/1750803/… Commented May 7, 2019 at 12:37
  • my solution to get the IP address from the MAC is here stackoverflow.com/questions/60571731 Commented Mar 9, 2020 at 11:51

3 Answers 3

4

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])
Sign up to request clarification or add additional context in comments.

1 Comment

If you haven't sent to the IP, it won't be in the ARP cache yet.
0

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

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.