Skip to content

Instantly share code, notes, and snippets.

@hitsumabushi
Created June 21, 2014 23:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hitsumabushi/81f1e18ad145ce70ffcf to your computer and use it in GitHub Desktop.
Python3
import fcntl, struct, socket
def getHwAddr(ifname):
'Return MAC address of given NIC'
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
info = fcntl.ioctl(s.fileno(), 0x8927,
struct.pack('256s', bytes(ifname[:15], 'ascii')))
return ''.join(['%02x:' % char for char in info[18:24]])[:-1]
except OSError:
return None
finally:
s.close()
if __name__ == "__main__":
print(getHwAddr('eth0'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment