Last active
September 24, 2019 08:24
-
-
Save angeloped/4c4af0fc47cad3ce6f0e6969799c225c to your computer and use it in GitHub Desktop.
Get the mac address of all available network interface. Netifaces alternative.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, psutil | |
from binascii import hexlify | |
# get mac address | |
def get_network_mac(interface, p=0): | |
# create dummy socket | |
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) | |
# bind it with interface name | |
s.bind((interface,p)) | |
# extract mac address | |
mac = hexlify(s.getsockname()[4]) | |
# close socket | |
s.close() | |
#return value | |
return mac | |
# list all available network interfaces | |
def list_all_netifaces(): | |
return psutil.net_if_addrs().keys() | |
# print them all | |
for ifaces in list_all_netifaces(): | |
print("{0}: {1}".format(ifaces, get_network_mac(ifaces))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment