forked from OrneLibrary/DeepOne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepOne.py
95 lines (85 loc) · 3.39 KB
/
deepOne.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
import os
import re
import math
import argparse
import ipaddress
from pathlib import Path
from collections import deque
import logging
# Pre-compile regex patterns
regex_patterns = {
'cidr': re.compile(r'^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$'),
'lastOctet': re.compile(r'^(\d{1,3}\.){3}\d{1,3}-\d{1,3}$'),
'allOctets': re.compile(r'^(\d{1,3}\.){3}(\d{1,3})-(\d{1,3}\.){3}\d{1,3}$'),
'single': re.compile(r'^(\d{1,3}\.){3}(\d{1,3})$')
}
def expand(ipRange):
scope = []
if regex_patterns['cidr'].search(ipRange):
net = ipaddress.ip_network(ipRange)
for adder in net:
scope.append(str(ipaddress.IPv4Address(adder)))
elif regex_patterns['lastOctet'].search(ipRange):
adder = ipRange.split('.')
startEnd = adder[3].split('-')
start = int(startEnd[0])
end = int(startEnd[1])
for i in range(start, end + 1):
scope.append(adder[0] + '.' + adder[1] + '.' + adder[2] + '.' + str(i))
elif regex_patterns['allOctets'].search(ipRange):
startEnd = ipRange.split('-')
start_ip = ipaddress.IPv4Address(startEnd[0])
end_ip = ipaddress.IPv4Address(startEnd[1])
for ip_int in range(int(start_ip), int(end_ip) + 1):
scope.append(str(ipaddress.IPv4Address(ip_int)))
elif regex_patterns['single'].search(ipRange):
scope.append(ipRange)
else:
logging.warning("Cannot parse: " + ipRange)
return []
return scope
def process_file(input_file_path, exclude_set, output_file_path):
with open(input_file_path, 'r') as inputFile, open(output_file_path, 'a') as outputFile:
for case in inputFile:
case = case.rstrip("\n")
for ip in expand(case):
if ip not in exclude_set:
outputFile.write(ip + "\n")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', required=True, help='Path to input file')
parser.add_argument('-e', '--exclude', help='Path to exclude file')
parser.add_argument('-o', '--output', default=os.getcwd() + "/output.txt", help='Output file')
parser.add_argument('-s', '--split', type=int, default=1, help='Number of files to split results into')
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
# Convert exclude list to a set for efficient lookup
exclude_set = set()
if args.exclude:
try:
with open(args.exclude, 'r') as excludeFile:
for line in excludeFile:
line = line.strip()
exclude_set.update(expand(line))
except IOError:
logging.error("Failed to read exclude file")
# Process input file
process_file(args.input, exclude_set, Path(args.output))
# File splitting logic
if args.split > 1:
allIps = deque([])
with open(args.output, 'r') as fullList:
for line in fullList:
allIps.append(line)
numPerFile = math.ceil(len(allIps)/args.split)
print(len(allIps))
print(numPerFile)
for fileNum in range(args.split):
with open(Path(str(args.output) + str(fileNum)), 'a') as outputFile:
for _ in range(numPerFile):
if allIps:
print(allIps.popleft().rstrip("\n"), file=outputFile)
else:
break
if __name__ == "__main__":
main()