-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipclear.py
50 lines (44 loc) · 1.31 KB
/
ipclear.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
import sys
import requests
from ipaddress import ip_network, ip_address
def output_valid_ips(ips):
ipvs4 = "https://www.cloudflare.com/ips-v4"
ipvs6 = "https://www.cloudflare.com/ips-v6"
ipranges = requests.get(ipvs4).text.split("\n")[:-1] # removing last trailing space
ipranges += requests.get(ipvs6).text.split("\n")[
:-1
] # removing last trailing space
nets = []
for iprange in ipranges:
nets.append(ip_network(iprange))
valid_ips = []
for ip in ips:
if ip == "": # skip empty line
continue
valid = True
for net in nets:
if ip_address(ip) in net:
valid = False
break
if valid:
valid_ips.append(ip)
return valid_ips
if __name__ == "__main__":
if len(sys.argv) < 3:
print(
"""
Usage : python {} input_file_path output_file_path
""".format(
__file__
)
)
sys.exit(1)
file_name, output_file = sys.argv[1], sys.argv[2]
with open(file_name) as f:
ips = f.read().split("\n")
valid_ips = output_valid_ips(ips)
with open(output_file, "w") as f:
for ip in valid_ips[:-1]:
f.write(ip + "\n")
# no new line after last line
f.write(valid_ips[-1])