Skip to content

Instantly share code, notes, and snippets.

@glides
Last active December 18, 2015 10:38
Show Gist options
  • Save glides/5769464 to your computer and use it in GitHub Desktop.
Save glides/5769464 to your computer and use it in GitHub Desktop.
Python: Get network interfaces
def all_interfaces():
max_possible = 128
bytes = max_possible * 32
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
names = array.array('B', '\0' * bytes)
outbytes = struct.unpack('iL', fcntl.ioctl(s.fileno(), 0x8912, struct.pack('iL', bytes, names.buffer_info()[0])))[0]
namestr = names.tostring()
lst = []
for i in range(0, outbytes, 40):
name = namestr[i:i + 16].split('\0', 1)[0]
ip = namestr[i + 20:i + 24]
lst.append((name, ip))
return lst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment