Created
June 21, 2014 23:12
-
-
Save hitsumabushi/81f1e18ad145ce70ffcf to your computer and use it in GitHub Desktop.
Python3
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 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