-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
155 lines (135 loc) · 5.04 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# coding=utf-8
# author: Reggie
# time: 2019/09/26 16:15
import platform
import re
import socket
import subprocess
class From:
def __init__(self, src):
self.src = src
def to_list(self):
return list(self.src)
def to_order_set_list(self, reverse=False):
tmp = self.to_list()
return sorted(set(tmp), key=tmp.index, reverse=reverse)
def map(self, func):
return From(map(func, self.src))
def filter(self, predicate):
return From(filter(predicate, self.src))
def get_host_ip():
"""返回本机IP地址"""
ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
ss.connect(('8.8.8.8', 8070))
ip = ss.getsockname()[0]
finally:
ss.close()
return ip
def get_all_address(family="ipv4"):
"""
family: ipv4 or ipv6
:return:
"""
if family == "ipv4":
family = socket.AF_INET
elif family == "ipv6":
family = socket.AF_INET6
else:
return []
hostname = socket.gethostname()
# family: AF_INET ipv4、AF_INET6 ipv6, None 为所有
addrlist = socket.getaddrinfo(hostname, None, family=family)
return [item[4][0] for item in addrlist]
def find_all_ip(platform):
ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
if platform == "Darwin" or platform == "Linux":
ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
output = ipconfig_process.stdout.read()
ip_pattern = re.compile('(inet %s)' % ipstr)
if platform == "Linux":
ip_pattern = re.compile('(inet addr:%s)' % ipstr)
pattern = re.compile(ipstr)
iplist = []
for ipaddr in re.finditer(ip_pattern, str(output)):
ip = pattern.search(ipaddr.group())
if ip.group() != "127.0.0.1":
iplist.append(ip.group())
return iplist
elif platform == "Windows":
ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE)
output = ipconfig_process.stdout.read()
ip_pattern = re.compile("IPv4 Address(\. )*: %s" % ipstr)
pattern = re.compile(ipstr)
iplist = []
for ipaddr in re.finditer(ip_pattern, str(output)):
ip = pattern.search(ipaddr.group())
if ip.group() != "127.0.0.1":
iplist.append(ip.group())
return iplist
def find_all_mask(platform):
ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
maskstr = '0x([0-9a-f]{8})'
if platform == "Darwin" or platform == "Linux":
ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
output = ipconfig_process.stdout.read()
mask_pattern = re.compile('(netmask %s)' % maskstr)
pattern = re.compile(maskstr)
if platform == "Linux":
mask_pattern = re.compile(r'Mask:%s' % ipstr)
pattern = re.compile(ipstr)
masklist = []
for maskaddr in mask_pattern.finditer(str(output)):
mask = pattern.search(maskaddr.group())
if mask.group() != '0xff000000' and mask.group() != '255.0.0.0':
masklist.append(mask.group())
return masklist
elif platform == "Windows":
ipconfig_process = subprocess.Popen("ipconfig", stdout=subprocess.PIPE)
output = ipconfig_process.stdout.read()
mask_pattern = re.compile(r"Subnet Mask (\. )*: %s" % ipstr)
pattern = re.compile(ipstr)
masklist = []
for maskaddr in mask_pattern.finditer(str(output)):
mask = pattern.search(maskaddr.group())
if mask.group() != '255.0.0.0':
masklist.append(mask.group())
return masklist
def get_broad_addr(ipstr, maskstr):
iptokens = map(int, ipstr.split("."))
masktokens = map(int, maskstr.split("."))
broadlist = []
for i in range(len(iptokens)):
ip = iptokens[i]
mask = masktokens[i]
broad = ip & mask | (~mask & 255)
broadlist.append(broad)
return '.'.join(map(str, broadlist))
def find_all_broad(platform):
ipstr = '([0-9]{1,3}\.){3}[0-9]{1,3}'
if platform == "Darwin" or platform == "Linux":
ipconfig_process = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
output = (ipconfig_process.stdout.read())
broad_pattern = re.compile('(broadcast %s)' % ipstr)
if platform == "Linux":
broad_pattern = re.compile(r'Bcast:%s' % ipstr)
pattern = re.compile(ipstr)
broadlist = []
for broadaddr in broad_pattern.finditer(str(output)):
broad = pattern.search(broadaddr.group())
broadlist.append(broad.group())
return broadlist
elif platform == "Windows":
iplist = find_all_ip(platform)
masklist = find_all_mask(platform)
broadlist = []
for i in range(len(iplist)):
broadlist.append(get_broad_addr(iplist[i], masklist[i]))
return broadlist
if __name__ == '__main__':
print(get_host_ip())
system = platform.system()
print(find_all_ip(system))
print(find_all_mask(system))
print(find_all_broad(system))
print(get_all_address())