4

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!

xoani
  • 107
  • 1
  • 1
  • 13
  • 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? – Daniel Pryden May 07 '19 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? – xoani May 07 '19 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. – Daniel Pryden May 07 '19 at 12:19
  • Perhaps this https://stackoverflow.com/questions/1750803/obtain-mac-address-from-devices-using-python – smitty_werbenjagermanjensen May 07 '19 at 12:37
  • my solution to get the IP address from the MAC is here https://stackoverflow.com/questions/60571731 – M Newton Mar 09 '20 at 11:51

3 Answers3

3

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])
NAGA RAJ S
  • 452
  • 4
  • 12
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
etskinner
  • 160
  • 1
  • 7
-2

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.

desmaxi
  • 293
  • 1
  • 13