P-P
P-P

Reputation: 1700

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

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.

Upvotes: 1

Views: 332

Answers (2)

P-P
P-P

Reputation: 1700

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.

Upvotes: 1

wasif
wasif

Reputation: 15478

Use netifaces:

import netifaces
iface = netifaces.ifaddresses('YOUR NETWORK INTERFACE NAME')[netifaces.AF_LINK]
print(iface['addr'])

Upvotes: 2

Related Questions