0

I have searched for an identifier and I tried to find mac address for any connected device on my server but the problem is that the output returns empty for example

a = os.popen("arp -a 192.168.6.150 | awk '{print $4}'").readlines()

a is empty

I'm working on captive portal page for untangle. I want to get the mac address from ip of device on network. This code runs on Apache server.

from mod_python import apache
from mod_python import util
Ahmed
  • 35
  • 2
  • 10
  • 2
    possible duplicate of [Getting MAC Address](http://stackoverflow.com/questions/159137/getting-mac-address) –  Aug 26 '13 at 14:29
  • 1
    I think the OP wants to get the MAC address of any device in the network, given its IP address (not the MAC of the machine running the code) – Don Aug 26 '13 at 15:26
  • The solution is here : http://stackoverflow.com/a/1750931/2615399 – Organ Aug 26 '13 at 15:32
  • Is the IP in the same subnet as your server? – FatalError Aug 26 '13 at 22:39
  • yes the IP in the same subnet, the problem is that all commands runs on terminal but not run on the page – Ahmed Aug 27 '13 at 11:23
  • all arp commands not run on the page – Ahmed Aug 27 '13 at 11:25
  • There are many helpful answers here! [How can I get the IP address of eth0 in Python?](https://stackoverflow.com/q/24196932/3904031) – uhoh Apr 11 '18 at 22:08

3 Answers3

4

The following function returns the mac or None if mac is not found.

import commands
def getmac(iface):
    mac = commands.getoutput("ifconfig " + iface + "| grep HWaddr | awk '{ print $5 }'")
    if len(mac)==17:
        return mac

getmac('eth0')
Simon Sagi
  • 494
  • 2
  • 6
1
import re, uuid


print (' : '.join(re.findall('..', '%012x' % uuid.getnode())))
Bugs
  • 4,491
  • 9
  • 32
  • 41
  • The original problem is that the code doesn't run when requested from a webpage. It has little to do with the parsing method. –  Jun 27 '17 at 12:05
0

You can also use python scapy module for the same

from scapy.all import *
def get_mac(ip_address):
    responses,unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address),timeout=2,retry=10)
# return the MAC address from a response
    for s,r in responses:
        return r[Ether].src
    return None
get_mac("192.168.31.14")
Ajay Kumar K K
  • 321
  • 2
  • 7