Skip to content

Instantly share code, notes, and snippets.

@vicendominguez
Last active December 17, 2015 15:50
Show Gist options
  • Save vicendominguez/0fe9acfe850a9c4a8d63 to your computer and use it in GitHub Desktop.
Save vicendominguez/0fe9acfe850a9c4a8d63 to your computer and use it in GitHub Desktop.
Get a name from the publicip and macaddrs
#!/usr/bin/python
import fcntl, socket, struct, urllib2
def getHwAddr(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return '_'.join(['%02x' % ord(char) for char in info[18:24]])
def getPublicAddr():
r = urllib2.urlopen("http://ipinfo.io/ip")
ip = r.read(100)
return ip[:-1]
print getPublicAddr() + "-" + getHwAddr('eth0')
root@raspberry:~# /usr/local/bin/get_macname.py
84.126.70.xx-b8_xx_eb_a4_xx_xc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment