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
7
  • 2
    possible duplicate of Getting MAC Address Commented Aug 26, 2013 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) Commented Aug 26, 2013 at 15:26
  • The solution is here : stackoverflow.com/a/1750931/2615399 Commented Aug 26, 2013 at 15:32
  • Is the IP in the same subnet as your server? Commented Aug 26, 2013 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 Commented Aug 27, 2013 at 11:23

3 Answers 3

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')
Sign up to request clarification or add additional context in comments.

Comments

1
import re, uuid


print (' : '.join(re.findall('..', '%012x' % uuid.getnode())))

1 Comment

The original problem is that the code doesn't run when requested from a webpage. It has little to do with the parsing method.
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.