Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import platform
  2. import socket
  3. import fcntl
  4. import struct
  5.  
  6. def getLocalIp():
  7. ips = []
  8. ifnames = []
  9. SIOCGIFADDR = 0x8915
  10. SIOCGIFBRDADDR = 0x8919
  11. SIOCGIFNETMASK = 0x891b
  12.  
  13. if platform.system().lower() == "darwin":
  14. SIOCGIFADDR = 0xc0206921
  15. SIOCGIFBRDADDR = 0xc0206923
  16. SIOCGIFNETMASK = 0xc0206925
  17.  
  18. from ctypes import (
  19. Structure, Union, POINTER,
  20. pointer, get_errno, cast,
  21. c_ushort, c_byte, c_void_p, c_char_p, c_uint,
  22. c_int, c_uint16, c_uint32
  23. )
  24. import ctypes.util
  25. import ctypes
  26.  
  27. class struct_sockaddr(Structure):
  28. _fields_ = [
  29. ('sa_family', c_ushort),
  30. ('sa_data', c_byte * 14)
  31. ]
  32.  
  33. class struct_sockaddr_in(Structure):
  34. _fields_ = [
  35. ('sin_family', c_ushort),
  36. ('sin_port', c_uint16),
  37. ('sin_addr', c_byte * 4)
  38. ]
  39.  
  40. class struct_sockaddr_in6(Structure):
  41. _fields_ = [
  42. ('sin6_family', c_ushort),
  43. ('sin6_port', c_uint16),
  44. ('sin6_flowinfo', c_uint32),
  45. ('sin6_addr', c_byte * 16),
  46. ('sin6_scope_id', c_uint32)]
  47.  
  48. class union_ifa_ifu(Union):
  49. _fields_ = [
  50. ('ifu_broadaddr', POINTER(struct_sockaddr)),
  51. ('ifu_dstaddr', POINTER(struct_sockaddr))]
  52.  
  53. class struct_ifaddrs(Structure):
  54. pass
  55.  
  56. struct_ifaddrs._fields_ = [
  57. ('ifa_next', POINTER(struct_ifaddrs)),
  58. ('ifa_name', c_char_p),
  59. ('ifa_flags', c_uint),
  60. ('ifa_addr', POINTER(struct_sockaddr)),
  61. ('ifa_netmask', POINTER(struct_sockaddr)),
  62. ('ifa_ifu', union_ifa_ifu),
  63. ('ifa_data', c_void_p)]
  64.  
  65. libc = ctypes.CDLL(ctypes.util.find_library('c'))
  66.  
  67. def ifap_iter(ifap):
  68. ifa = ifap.contents
  69. while True:
  70. yield ifa
  71. if not ifa.ifa_next:
  72. break
  73. ifa = ifa.ifa_next.contents
  74.  
  75. def get_network_interfaces():
  76. ifap = POINTER(struct_ifaddrs)()
  77. result = libc.getifaddrs(pointer(ifap))
  78. if result != 0:
  79. raise OSError(get_errno())
  80. del result
  81. try:
  82. retval = []
  83. for ifa in ifap_iter(ifap):
  84. name = ifa.ifa_name.decode("UTF-8")
  85. retval.append(name)
  86. return sorted(set(retval), key=retval.index)
  87. finally:
  88. libc.freeifaddrs(ifap)
  89. ifnames = get_network_interfaces()
  90. else:
  91. is_64bits = sys.maxsize > 2**32
  92. struct_size = 40 if is_64bits else 32
  93. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  94. max_possible = 8
  95. while True:
  96. _bytes = max_possible * struct_size
  97. names = array.array('B', b'\0' * _bytes)
  98. outbytes = struct.unpack('iL', fcntl.ioctl(
  99. s.fileno(),
  100. 0x8912,
  101. struct.pack('iL', _bytes, names.buffer_info()[0])
  102. ))[0]
  103.  
  104. if outbytes == _bytes:
  105. max_possible *= 2
  106. else:
  107. break
  108. namestr = names.tostring()
  109. for i in range(0, outbytes, struct_size):
  110. ifnames.append((namestr[i:i+16].split(b'\0', 1)[0]).decode("utf-8"))
  111. s.close()
  112.  
  113. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  114.  
  115. for ifn in ifnames:
  116. try:
  117. _addr = fcntl.ioctl(s.fileno(), SIOCGIFADDR, struct.pack('256s', ifn[:15].encode('utf-8')))
  118. iface_addr = socket.inet_ntoa(_addr[20:24])
  119. _brdaddr = fcntl.ioctl(s.fileno(), SIOCGIFBRDADDR, struct.pack('256s', ifn[:15].encode('utf-8')))
  120. iface_brdaddr = socket.inet_ntoa(_brdaddr[20:24])
  121. _netmask = fcntl.ioctl(s.fileno(), SIOCGIFNETMASK, struct.pack('256s', ifn[:15].encode('utf-8')))
  122. iface_netmask = socket.inet_ntoa(_brdaddr[20:24])
  123. ips.append({"ifname": ifn, "addr": iface_addr, "broadcast": iface_brdaddr, "netmask": iface_netmask})
  124. except Exception as e:
  125. # print(e)
  126. pass
  127. s.close()
  128.  
  129. return ips
  130.  
  131. print(getLocalIp())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement