Get remote MAC address using Python and Linux
Solution 1
Use these commands:
arp -n <IP Address>|awk '/<ip address>/ {print $3}'
for example, if you want mac address of 192.168.10.1:
#arp -n 192.168.10.1|awk '/192.168.10.1/ {print $3}'
#00:0c:29:68:8f:a4
Solution 2
You can try running command arp -a
Here is few links about Mac Address grabbing (not tested)
In Linux/Unix, arping,
http://www.ibm.com/developerworks/aix/library/au-pythocli/
In Windows, using IP Helper API through ctypes
http://code.activestate.com/recipes/347812/
Solution 3
arp entries might never be right, I tried to ping a host several times but arp -a would not give me it's mac/ethernet address. (No worry with the windows code from active state BTW)
The reliable way on Linux (and *nix) is to use arping or scappy (see http://en.wikipedia.org/wiki/Arping) and then parse the output. Here's the code I used. You have to be root or use sudo to run arping.
cmd = '/sbin/arping -c 1 ' + remotehost
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, errors = p.communicate()
if output is not None :
mac_addr = re.findall(r'(\[.*\])', output)[0].replace('[', '').replace(']', '')
Solution 4
If you just want to query the OS' arp cache, reasonably recent linux kernels support this:
import os, sys
host = sys.argv[1]
# ping is optional (sends a WHO_HAS request)
os.popen('ping -c 1 %s' % host)
# grep with a space at the end of IP address to make sure you get a single line
fields = os.popen('grep "%s " /proc/net/arp' % host).read().split()
if len(fields) == 6 and fields[3] != "00:00:00:00:00:00":
print fields[3]
else:
print 'no response from', host
Related videos on Youtube
myk_raniu
Graduated from Electronic Engineering in University College Cork. 6 Years Experience Software testing and Script Coding at Xilinx Ireland Learning Data Science Techniques and R Studio
Updated on May 05, 2020Comments
-
myk_raniu about 4 years
How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.
-
C. K. Young over 14 yearsOr,
arp -n $IP
, to filter to just the desired IP address. -
YOU over 14 yearsThanks, In Windows,
arp -a $IP
just a note. -
Nick Bastin over 14 yearsKeep in mind that this won't send a WHO_HAS message on the network - the address has to have been seen by your machine already, which it might not be if you have a large network (also old entries can get deleted if the cache gets too large).
-
YOU over 14 yearsYes, Correct, for that case, may be should try to do something first like pinging, and find out arp cache.
-
myk_raniu over 14 yearsSo you reckon manualy execute the ping and arp commands using os.popen? There isn't any python modules to do this?
-
YOU over 14 yearsI dont see arp on standard module
-
Andrew McGregor over 14 yearsYou can, of course, implement ARP yourself (it's RFC 826), build an arping style packet, open a raw socket, and send it yourself. But why do all the work to implement that yourself when you can just use the system component to do it?
-
Nightforce2 over 6 yearsSo in a sense. This example is incomplete.
-
bitinerant over 5 yearsAt least on Ubuntu 18.04, you do not have to be root to run
arping
.