How to get several eternet mac address by python at windows?

260 Views Asked by At

I'm trying to get several eternet mac addresses by python.
I tried this python code, and it works to get the first mac address.

import getmac
print(getmac.get_mac_address())

In my computer, I installed Virtual Box, and it installed 2 virtual eternal adapters. I want to get all mac address lists which include Virtual Box(or Hyper-V) mac address with Python.

I'm using Windows10 x64.
Sincerely Ryan.

2

There are 2 best solutions below

0
On BEST ANSWER

Use netifaces:

import netifaces
iface = netifaces.ifaddresses('YOUR NETWORK INTERFACE NAME')[netifaces.AF_LINK]
print(iface['addr'])
0
On
import netifaces
for nic in netifaces.interfaces():
    iface = netifaces.ifaddresses(nic)[netifaces.AF_LINK]
    if len(iface[0]['addr']) > 0:
        print("mac address: ", iface[0]['addr'])

Thanks, Wasif Hasan.