Return all interfaces that are up
()
| 86 | |
| 87 | # from: http://code.activestate.com/recipes/439093/ |
| 88 | def all_interfaces(): |
| 89 | ''' |
| 90 | Return all interfaces that are up |
| 91 | ''' |
| 92 | import fcntl # Linux only, so only import when required |
| 93 | |
| 94 | is_64bits = sys.maxsize > 2**32 |
| 95 | struct_size = 40 if is_64bits else 32 |
| 96 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 97 | max_possible = 8 # initial value |
| 98 | while True: |
| 99 | bytes = max_possible * struct_size |
| 100 | names = array.array('B', b'\0' * bytes) |
| 101 | outbytes = struct.unpack('iL', fcntl.ioctl( |
| 102 | s.fileno(), |
| 103 | 0x8912, # SIOCGIFCONF |
| 104 | struct.pack('iL', bytes, names.buffer_info()[0]) |
| 105 | ))[0] |
| 106 | if outbytes == bytes: |
| 107 | max_possible *= 2 |
| 108 | else: |
| 109 | break |
| 110 | namestr = names.tostring() |
| 111 | return [(namestr[i:i+16].split(b'\0', 1)[0], |
| 112 | socket.inet_ntoa(namestr[i+20:i+24])) |
| 113 | for i in range(0, outbytes, struct_size)] |
| 114 | |
| 115 | def addr_to_hex(addr): |
| 116 | ''' |