Skip to content

Instantly share code, notes, and snippets.

@GhostofGoes
Last active May 5, 2025 21:58
Show Gist options
  • Save GhostofGoes/0a8e82930e75afcefbd879a825ba4c26 to your computer and use it in GitHub Desktop.
Save GhostofGoes/0a8e82930e75afcefbd879a825ba4c26 to your computer and use it in GitHub Desktop.
Get MAC addresses using a variety of Python packages.
# **************************************
# ** Get MAC address of a remote host **
def arpreq_ip(ip):
# type: (str) -> Optional[str]
import arpreq
return arpreq.arpreq('192.168.1.1')
def scapy_ip(ip):
# type: (str) -> str
"""Requires root permissions on POSIX platforms.
Windows does not have this limitation."""
from scapy.layers.l2 import getmacbyip
return getmacbyip(ip)
# ******************************************
# ******************************************
# ** Get MAC address of a local interface **
def psutil_iface(iface):
# type: (str) -> Optional[str]
import psutil
nics = psutil.net_if_addrs()
if iface in nics:
nic = nics[iface]
for i in nic:
if i.family == psutil.AF_LINK:
return i.address
def netifaces_iface(iface):
# type: (str) -> str
import netifaces
return netifaces.ifaddresses(iface)[netifaces.AF_LINK][0]['addr']
def scapy_iface(iface):
# type: (str) -> str
# Note: if the interface exists, but has no MAC,
# get_if_hwaddr will return "00:00:00:00:00:00
from scapy.layers.l2 import get_if_hwaddr
return get_if_hwaddr(iface)
# ********************************************
# ********************************************
# Determine the default interface for a system
def netifaces_default_gateway():
# type: () -> str
import netifaces
return list(netifaces.gateways()['default'].values())[0][1]
# ********************************************
# ********************************************
# Old versions of scapy required this on Windows to get a local interface
# (Not sure exact version this was changed, but likely pre-2.6.)
def old_scapy_iface_windows(iface):
# type: (str) -> str
from scapy.layers.l2 import get_if_hwaddr
if WINDOWS:
from scapy.arch.windows import get_windows_if_list
interfaces = get_windows_if_list()
for i in interfaces:
if any(iface in i[x] for x in
['name', 'netid', 'description', 'win_index']):
return i['mac']
# WARNING: Do not put an 'else' here!
return get_if_hwaddr(iface)
# ********************************************
@gpotter2
Copy link

The if WINDOWS: isn't required for iface. on Scapy. get_if_hwaddr works as well.

@GhostofGoes
Copy link
Author

The if WINDOWS: isn't required for iface. on Scapy. get_if_hwaddr works as well.

@gpotter2 Thanks for the suggestion! What version of scapy was that added in? This gist is pretty old and definitely should be updated.

Also, thanks for all of your excellent work on Scapy! I've been using Scapy for many years now and it's easily in top 5 Python libraries I've ever used in terms of design, ease of use, and how well it's maintained.

@gpotter2
Copy link

Thanks. Unfortunately I have no idea about the version ^^'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment