Get remote MAC address using Python and Linux

How to get the MAC address of a remote host on a local network? I use Python and Linux.

+5
source share
7 answers

You can try to execute the command arp -a

Here are some Mac address capture links (not verified)

On Linux / Unix, arping,

http://www.ibm.com/developerworks/aix/library/au-pythocli/

On Windows using IP Helper API via ctypes

http://code.activestate.com/recipes/347812/

+4
source

Use the following commands:

arp -n <IP Address>|awk '/<ip address>/ {print $3}'

for example if you want mac address 192.168.10.1:

#arp -n 192.168.10.1|awk '/192.168.10.1/ {print $3}'
#00:0c:29:68:8f:a4
+3
source

arp- , , arp -a mac/ethernet. ( Windows BTW)

Linux ( * nix) - arping scappy (. http://en.wikipedia.org/wiki/Arping), , , . root sudo 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(']', '')      
+2

- , Linux :

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
+1

. MAC-, , . "nbtstat" Windows.

Unix "nbtscan", . , , NetBIOS python, ?

0

win32 linux

import subprocess
import sys
remotehost="192.168.0.122"
cmd="arp -a"
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, errors = p.communicate()
if output is not None :
    if sys.platform in ['linux','linux2']:
        for i in output.split("\n"):
            if remotehost in i:
                for j in i.split():
                    if ":" in j:
                        print "%s--> %s" % (remotehost,j)
    elif sys.platform in ['win32']:
        item =  output.split("\n")[-2]
        if remotehost in item:
            print "%s-->  %s" %(remotehost, item.split()[1])

NB: arp , " ", , ping, arp -a .

0

, python3

import subprocess
import sys
remotehost="192.168.0.122"
cmd="arp -a"
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, errors = p.communicate()

if output is not None :
    output = output.decode('ascii')
    if sys.platform in ['linux','linux2']:
        for i in output.split("\n"):
            if remotehost in i:
                for j in i.split():
                    if ":" in j:
                        print( "%s--> %s" % (remotehost,j))
    elif sys.platform in ['win32']:
        item =  output.split("\n")[-2]
        if remotehost in item:
            print("%s-->  %s" %(remotehost, item.split()[1]))
0

All Articles