-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_ip.py
70 lines (58 loc) · 2.07 KB
/
get_ip.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import os
import ipaddress
import logging
class ipFactory:
def __init__(self, q, IprangeStr):
self.q = q
self.ipRange = IprangeStr.split('\n')
self.iprangelen = IprangeStr.count('\n')
self.nowIndex = self.q.get()
self.deadLine = self.nowIndex
self.q.put((self.nowIndex + 1) % self.iprangelen)
self.generateIP()
self.GoodRange = set()
self.getFromGood = True
self.getGoodRange()
print("Now Index:%4d" % self.nowIndex)
def getGoodRange(self):
if not self.getFromGood:
self.GoodRange = set()
else:
with open("find_log0.txt") as f:
s = f.readlines()
self.GoodRange = set([int(i.split(":")[0]) for i in s])
self._GoodRange = self.GoodRange.copy()
def generateIP(self):
l1 = [[ipaddress.ip_address(ip.split('-')[0]),
ipaddress.ip_address(ip.split('-')[-1])]
for ip in self.ipRange if '-' in ip]
l2 = [[ipaddress.ip_network(net)[0],
ipaddress.ip_network(net)[-1]]
for net in self.ipRange if '/' in net]
self.ipList = l1 + l2
self.nowIp = self.ipList[self.nowIndex][0]
def getIp(self):
while self.nowIp > self.ipList[self.nowIndex][-1]:
self.nowIndex = self.increaseIndex()
self.nowIp = self.ipList[self.nowIndex][0]
print("Now Index:%4d" % self.nowIndex)
t = self.nowIp
self.nowIp += 1
return str(t)
def increaseIndex(self):
while True:
if len(self.GoodRange) > 0:
nowIndex = self.GoodRange.pop()
return nowIndex
else:
nowIndex = self.q.get()
if nowIndex == self.deadLine:
raise SystemExit
self.q.put((nowIndex + 1) % self.iprangelen)
if nowIndex not in self._GoodRange:
return nowIndex
def getIndex(self):
return self.nowIndex