Uses the SIOCGIFCONF ioctl to obtain a list of interfaces and extracts those names, returning them in a list of strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import socket import fcntl import struct import array def all_interfaces(): max_possible = 128 # arbitrary. raise if needed. 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, # SIOCGIFCONF struct.pack('iL', bytes, names.buffer_info()) )) namestr = names.tostring() return [namestr[i:i+32].split('\0', 1) for i in range(0, outbytes, 32)]
This solution should be faster than running ifconfig and parsing its output, and simpler than reading and parsing /proc/net/dev, since that will list all existing interfaces, whether active (up) or not.
This solution probably only works on Linux-- and possibly not all versions of even that-- since it depends on some C structures having a particular size and layout, as well as having the SIOCGIFCONF ioctl number (0x8912) hardcoded. I believe it could be adjusted, though, to work on other Unix systems.