2017-08-25 22:47:08 +08:00
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import array
|
|
|
|
|
|
|
|
import struct
|
2017-11-27 21:20:59 +08:00
|
|
|
import ipaddress
|
2017-08-29 19:12:07 +08:00
|
|
|
from netifaces import interfaces, ifaddresses, AF_INET
|
|
|
|
|
2017-08-25 22:47:08 +08:00
|
|
|
__author__ = 'Barak'
|
|
|
|
|
|
|
|
|
|
|
|
# Local ips function
|
|
|
|
if sys.platform == "win32":
|
|
|
|
def local_ips():
|
|
|
|
local_hostname = socket.gethostname()
|
|
|
|
return socket.gethostbyname_ex(local_hostname)[2]
|
|
|
|
else:
|
|
|
|
import fcntl
|
2017-09-21 20:55:02 +08:00
|
|
|
|
2017-08-25 22:47:08 +08:00
|
|
|
def local_ips():
|
|
|
|
result = []
|
|
|
|
try:
|
|
|
|
is_64bits = sys.maxsize > 2 ** 32
|
|
|
|
struct_size = 40 if is_64bits else 32
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
max_possible = 8 # initial value
|
|
|
|
while True:
|
|
|
|
struct_bytes = max_possible * struct_size
|
|
|
|
names = array.array('B', '\0' * struct_bytes)
|
|
|
|
outbytes = struct.unpack('iL', fcntl.ioctl(
|
|
|
|
s.fileno(),
|
|
|
|
0x8912, # SIOCGIFCONF
|
|
|
|
struct.pack('iL', struct_bytes, names.buffer_info()[0])
|
|
|
|
))[0]
|
|
|
|
if outbytes == struct_bytes:
|
|
|
|
max_possible *= 2
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
namestr = names.tostring()
|
|
|
|
|
|
|
|
for i in range(0, outbytes, struct_size):
|
|
|
|
addr = socket.inet_ntoa(namestr[i + 20:i + 24])
|
|
|
|
if not addr.startswith('127'):
|
|
|
|
result.append(addr)
|
|
|
|
# name of interface is (namestr[i:i+16].split('\0', 1)[0]
|
|
|
|
finally:
|
|
|
|
return result
|
|
|
|
# End of local ips function
|
2017-08-29 19:12:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def local_ip_addresses():
|
|
|
|
ip_list = []
|
|
|
|
for interface in interfaces():
|
|
|
|
addresses = ifaddresses(interface).get(AF_INET, [])
|
|
|
|
ip_list.extend([link['addr'] for link in addresses if link['addr'] != '127.0.0.1'])
|
|
|
|
return ip_list
|
2017-11-27 21:20:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_subnets():
|
|
|
|
subnets = []
|
|
|
|
for interface in interfaces():
|
|
|
|
addresses = ifaddresses(interface).get(AF_INET, [])
|
|
|
|
subnets.extend([ipaddress.ip_interface(link['addr'] + '/' + link['netmask']).network for link in addresses if link['addr'] != '127.0.0.1'])
|
|
|
|
return subnets
|