How do I get the MAC address of a remote host on my LAN? I'm using Python and Linux.
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/
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
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(']', '')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With