-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipParser.py
43 lines (37 loc) · 1.25 KB
/
ipParser.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
import ipaddress
def ipParser(iplist):
iplist = sorted(iplist)
newStr = ''
head = ''
nextip = ''
ipNewset = []
for ip in iplist:
if head == '':
head = ipaddress.ip_address(ip)
elif head != '':
nextip = ipaddress.ip_address(ip) - 1
if head == nextip:
ipNewset.append(head)
ipNewset.append(nextip+1)
head = nextip + 1
else:
if len(ipNewset) == 0:
newStr += f'{head},'
head = nextip + 1
elif len(ipNewset) == 1:
newStr += f'{head},'
head = nextip + 1
elif len(ipNewset) > 1:
newStr += f'{ipNewset[0]}-{ipNewset[len(ipNewset)-1]},'
ipNewset.clear()
head = nextip + 1
if len(ipNewset) == 0:
newStr += f'{head},'
elif len(ipNewset) == 1:
newStr += f'{ipNewset[0]},'
elif len(ipNewset) > 1:
newStr += f'{ipNewset[0]}-{ipNewset[len(ipNewset)-1]},'
ipNewset.clear()
return newStr
iplist = ['1.1.1.1','1.1.1.3','1.1.1.5','1.1.1.7','1.1.1.9']
print(ipParser(iplist))