1

I tried get Mac adders in python 3.6.2,i wrote code bellow to get it:

from uuid import getnode 
address=getnode()
h = iter(hex(address)[2:].zfill(12))
print(":".join(i + next(h) for i in h))

but it print mistake,for example,if my Mac adders is ff:ff:ff:ff:ff:12,it print ff:ff:ff:ff:ff:25. How fix it?

naghi
  • 253
  • 3
  • 8
  • 15
  • 1
    `uuid.getnode()` is not a good way to get the MAC **address**. It could return the address of any device in the system or even be random. – Klaus D. Aug 27 '17 at 06:41
  • https://stackoverflow.com/questions/159137/getting-mac-address – jophab Aug 27 '17 at 06:53
  • Recommend [this](https://stackoverflow.com/a/159150/4909087) from the dupe. – cs95 Aug 27 '17 at 07:05

1 Answers1

0

This can be achieved using the netifaces module.

  1. Import the module:

    >>> import netifaces
    
  2. Get all the interfaces present

    >>> netifaces.interfaces()  
    ['lo', 'eth0', 'tun2'] 
    
  3. Extract Mac address of selected interface eg. eth0

    >>> netifaces.ifaddresses('eth0')[netifaces.AF_LINK] 
    [{'addr': '00:00:00:00:00:00', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]
    
Chanchal Roshan
  • 164
  • 1
  • 3
  • 13