Ahmed
Ahmed

Reputation: 35

Find mac address on python

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

Upvotes: 0

Views: 7287

Answers (3)

Ajay Kumar K K
Ajay Kumar K K

Reputation: 341

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")

Upvotes: 0

user8129296
user8129296

Reputation:

import re, uuid


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

Upvotes: 1

Simon Sagi
Simon Sagi

Reputation: 494

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')

Upvotes: 4

Related Questions