-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
37 lines (28 loc) · 900 Bytes
/
main.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
# coding=utf-8
import os
import argparse
import ipaddress
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--blacklist', help='Black list of IP hosts/networks')
parser.add_argument('-w', '--whitelist', help='White list of IP hosts/networks')
return parser.parse_args()
def file2ipset(filename):
output = set()
if not os.path.isfile(filename):
return output
with open(filename) as __file:
for line in __file.readlines():
line = line.strip()
if '/' in line:
for ip in ipaddress.ip_network(line).hosts():
output.add(str(ip))
else:
output.add(line)
return output
def main():
args = parse_args()
for ip in (file2ipset(args.blacklist) - file2ipset(args.whitelist)):
print(ip)
if __name__ == '__main__':
main()