-
-
Save subzer0iq/31dcd5d975372c388cb4f9fe2c19b463 to your computer and use it in GitHub Desktop.
Python: List all Network Interfaces On Computer
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
""" | |
Determine IPv4 addresses on a Linux machine via the socket interface. | |
Thanks @bubthegreat the changes to make it Py2/3 compatible and the helpful | |
code comments: https://gist.github.com/pklaus/289646#gistcomment-2396272 | |
This version has all comments removed for brevity. | |
""" | |
import socket | |
import array | |
import struct | |
import fcntl | |
def get_local_interfaces(): | |
""" Returns a dictionary of name:ip key value pairs. """ | |
MAX_BYTES = 4096 | |
FILL_CHAR = b'\0' | |
SIOCGIFCONF = 0x8912 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
names = array.array('B', MAX_BYTES * FILL_CHAR) | |
names_address, names_length = names.buffer_info() | |
mutable_byte_buffer = struct.pack('iL', MAX_BYTES, names_address) | |
mutated_byte_buffer = fcntl.ioctl(sock.fileno(), SIOCGIFCONF, mutable_byte_buffer) | |
max_bytes_out, names_address_out = struct.unpack('iL', mutated_byte_buffer) | |
namestr = names.tobytes() | |
namestr[:max_bytes_out] | |
bytes_out = namestr[:max_bytes_out] | |
ip_dict = {} | |
for i in range(0, max_bytes_out, 40): | |
name = namestr[ i: i+16 ].split(FILL_CHAR, 1)[0] | |
name = name.decode('utf-8') | |
ip_bytes = namestr[i+20:i+24] | |
full_addr = [] | |
for netaddr in ip_bytes: | |
if isinstance(netaddr, int): | |
full_addr.append(str(netaddr)) | |
elif isinstance(netaddr, str): | |
full_addr.append(str(ord(netaddr))) | |
ip_dict[name] = '.'.join(full_addr) | |
return ip_dict | |
if __name__ == "__main__": | |
for iface, ip in get_local_interfaces().items(): | |
print("{ip:15s} {iface}".format(ip=ip, iface=iface)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment