Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import platform
- import socket
- import fcntl
- import struct
- def getLocalIp():
- ips = []
- ifnames = []
- SIOCGIFADDR = 0x8915
- SIOCGIFBRDADDR = 0x8919
- SIOCGIFNETMASK = 0x891b
- if platform.system().lower() == "darwin":
- SIOCGIFADDR = 0xc0206921
- SIOCGIFBRDADDR = 0xc0206923
- SIOCGIFNETMASK = 0xc0206925
- from ctypes import (
- Structure, Union, POINTER,
- pointer, get_errno, cast,
- c_ushort, c_byte, c_void_p, c_char_p, c_uint,
- c_int, c_uint16, c_uint32
- )
- import ctypes.util
- import ctypes
- class struct_sockaddr(Structure):
- _fields_ = [
- ('sa_family', c_ushort),
- ('sa_data', c_byte * 14)
- ]
- class struct_sockaddr_in(Structure):
- _fields_ = [
- ('sin_family', c_ushort),
- ('sin_port', c_uint16),
- ('sin_addr', c_byte * 4)
- ]
- class struct_sockaddr_in6(Structure):
- _fields_ = [
- ('sin6_family', c_ushort),
- ('sin6_port', c_uint16),
- ('sin6_flowinfo', c_uint32),
- ('sin6_addr', c_byte * 16),
- ('sin6_scope_id', c_uint32)]
- class union_ifa_ifu(Union):
- _fields_ = [
- ('ifu_broadaddr', POINTER(struct_sockaddr)),
- ('ifu_dstaddr', POINTER(struct_sockaddr))]
- class struct_ifaddrs(Structure):
- pass
- struct_ifaddrs._fields_ = [
- ('ifa_next', POINTER(struct_ifaddrs)),
- ('ifa_name', c_char_p),
- ('ifa_flags', c_uint),
- ('ifa_addr', POINTER(struct_sockaddr)),
- ('ifa_netmask', POINTER(struct_sockaddr)),
- ('ifa_ifu', union_ifa_ifu),
- ('ifa_data', c_void_p)]
- libc = ctypes.CDLL(ctypes.util.find_library('c'))
- def ifap_iter(ifap):
- ifa = ifap.contents
- while True:
- yield ifa
- if not ifa.ifa_next:
- break
- ifa = ifa.ifa_next.contents
- def get_network_interfaces():
- ifap = POINTER(struct_ifaddrs)()
- result = libc.getifaddrs(pointer(ifap))
- if result != 0:
- raise OSError(get_errno())
- del result
- try:
- retval = []
- for ifa in ifap_iter(ifap):
- name = ifa.ifa_name.decode("UTF-8")
- retval.append(name)
- return sorted(set(retval), key=retval.index)
- finally:
- libc.freeifaddrs(ifap)
- ifnames = get_network_interfaces()
- else:
- 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
- while True:
- _bytes = max_possible * struct_size
- names = array.array('B', b'\0' * _bytes)
- outbytes = struct.unpack('iL', fcntl.ioctl(
- s.fileno(),
- 0x8912,
- struct.pack('iL', _bytes, names.buffer_info()[0])
- ))[0]
- if outbytes == _bytes:
- max_possible *= 2
- else:
- break
- namestr = names.tostring()
- for i in range(0, outbytes, struct_size):
- ifnames.append((namestr[i:i+16].split(b'\0', 1)[0]).decode("utf-8"))
- s.close()
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- for ifn in ifnames:
- try:
- _addr = fcntl.ioctl(s.fileno(), SIOCGIFADDR, struct.pack('256s', ifn[:15].encode('utf-8')))
- iface_addr = socket.inet_ntoa(_addr[20:24])
- _brdaddr = fcntl.ioctl(s.fileno(), SIOCGIFBRDADDR, struct.pack('256s', ifn[:15].encode('utf-8')))
- iface_brdaddr = socket.inet_ntoa(_brdaddr[20:24])
- _netmask = fcntl.ioctl(s.fileno(), SIOCGIFNETMASK, struct.pack('256s', ifn[:15].encode('utf-8')))
- iface_netmask = socket.inet_ntoa(_brdaddr[20:24])
- ips.append({"ifname": ifn, "addr": iface_addr, "broadcast": iface_brdaddr, "netmask": iface_netmask})
- except Exception as e:
- # print(e)
- pass
- s.close()
- return ips
- print(getLocalIp())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement