Get remote MAC address using Python and Linux

22,455

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
Share:
22,455

Related videos on Youtube

myk_raniu
Author by

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, 2020

Comments

  • myk_raniu
    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
    C. K. Young over 14 years
    Or, arp -n $IP, to filter to just the desired IP address.
  • YOU
    YOU over 14 years
    Thanks, In Windows, arp -a $IP just a note.
  • Nick Bastin
    Nick Bastin over 14 years
    Keep 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
    YOU over 14 years
    Yes, Correct, for that case, may be should try to do something first like pinging, and find out arp cache.
  • myk_raniu
    myk_raniu over 14 years
    So you reckon manualy execute the ping and arp commands using os.popen? There isn't any python modules to do this?
  • YOU
    YOU over 14 years
    I dont see arp on standard module
  • Andrew McGregor
    Andrew McGregor over 14 years
    You 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
    Nightforce2 over 6 years
    So in a sense. This example is incomplete.
  • bitinerant
    bitinerant over 5 years
    At least on Ubuntu 18.04, you do not have to be root to run arping.