You could also do something similar to the verified answer. Layer III addresses are known. Sniff(prn=arp_monitor_callback, filter="arp", store=0) I'm looking for a way (with python) to obtain the layer II address from a device on my local network.
Return pkt.sprintf("%ARP.hwsrc% %ARP.psrc%") The return value is an IP respectively MAC instance if the given address is valid. Functions isIP and isMAC can be used to check an address. After that, paste the following in a new python script and replace 'yourinterfacename' with the name you got. After that is done, check the current interface's name in the terminal by typing.
Both functions provide an optional argument to exclude specific IP or MAC instances. Easiest way is by installing netifaces (prepend sudo if it doesn't work), Just do: pip install netifaces. If ARP in pkt and pkt.op in (1,2): #who-has or is-at Functions getIPs and getMACs collect the available IP respectively MAC addresses from the underlying system. You must either call a system command such as arp to get ARP information, or generate your own packets using Scapy.Įdit: An example using Scapy from their website: As mentioned in the answer chosen by the asker of that question, Python doesn't have a built-in way to do it. There was a similar question answered not too long ago on this site.
> # The time between ping and arp check must be small, as ARP may not cache long Functions for generating transforming and testing MAC addresses.
Knowing that, you do a little subprocess magic - otherwise you're writing ARP cache checking code yourself, and you don't want to do that: > from subprocess import Popen, PIPE Python library for working with Ethernet MAC addresses. That will place the target - as long as it's within your netmask, which it sounds like in this situation it will be - in your system's ARP cache. A small change to the regular expression will make it work in OS X.įirst, you must ping the target. I don't have Windows handy, so the following solution works on the Linux box I wrote it on. To answer the question with Python depends on your platform.