I am looking for a python way grab the client's MAC address. All requests are over the same network. I am looking for something similar to perform arp -n <Client_IP>
on the router.
Asked
Active
Viewed 9,546 times
3

SuicideSheep
- 5,260
- 19
- 64
- 117

cemclaug
- 35
- 1
- 8
-
2may be you can do this using this two links by first finding the IP of the machine you intend to find MAC of and then from IP find its MAC [find mac for ip](http://stackoverflow.com/questions/159137/getting-mac-address) and [find ip](http://stackoverflow.com/questions/3759981/get-ip-address-of-visitors-using-python-flask) – Yogesh D Mar 05 '14 at 03:49
-
I don't have much networking experience, so I don't see how I could combine those two answers. The first link provides a way to see the mac address of my own network interfaces and the second link provides a way to find the request user's ip. Could you explain how I could combine those? – cemclaug Mar 06 '14 at 18:32
3 Answers
3
Not sure but you can always get IP address using the request object like
request.remote_addr
and for that you have to
import request
and then you can pass this IP to this function
import netifaces as nif
def mac_for_ip(ip):
'Returns a list of MACs for interfaces that have given IP, returns None if not found'
for i in nif.interfaces():
addrs = nif.ifaddresses(i)
try:
if_mac = addrs[nif.AF_LINK][0]['addr']
if_ip = addrs[nif.AF_INET][0]['addr']
except IndexError, KeyError: #ignore ifaces that dont have MAC or IP
if_mac = if_ip = None
if if_ip == ip:
return if_mac
return None

Andrew Myers
- 2,754
- 5
- 32
- 40

Yogesh D
- 1,663
- 2
- 23
- 38
-
That didn't always worked for me. I connected a device "A" to a wifi router using a cable, this device runs with a flask-based HTTP server and calls mac_for_ip() for each request, then I connected another device "B" to the wifi router via wifi and made a request to the HTTP server running on "A". The MAC address of the client couldn't be determined. A solution that better worked for me was to look for the MAC address in the ARP table using the python_arptable package. – Jefferson Feb 09 '22 at 00:12
0
If you get the gateway then you'll get the mac address corresponding to the interface that is using that gateway:
>>> gateway_iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
>>> netifaces.ifaddresses(gateway_iface)[netifaces.AF_LINK][0]['addr']
'de:f2:fe:66:a4:8e'

Claudiu
- 224,032
- 165
- 485
- 680
0
You can get the IP address of a client using flask's request.remote_add
, then you can search for its MAC address in the ARP table of your network interface. The code below worked for me:
from flask import Flask, request
from python_arptable import get_arp_table
app = Flask(__name__)
def mac_from_ip(ip):
arp_table = get_arp_table()
for entry in arp_table:
if entry['IP address'] == ip:
return entry['HW address']
return None
@app.route('/')
def get_client_mac():
client_ip = request.remote_addr
client_mac = mac_for_ip(client_ip)
return client_mac

Jefferson
- 529
- 4
- 9