Última actividad 2 weeks ago

dominic revisó este gist 2 weeks ago. Ir a la revisión

1 file changed, 50 insertions

subl.py(archivo creado)

@@ -0,0 +1,50 @@
1 + #!/usr/bin/env python3
2 +
3 + """ Sort the blacklist ip list file into CIDR networks
4 +
5 + Author: Dominic Reich “OE7DRT”
6 + <quick.hat4396@qtztsjosmprqmgtunjyf.com>
7 +
8 + Usage: Run the program, paths are fixed (specified below)
9 +
10 + Unfinished and aborted - I don't need something like this any more.
11 + So i published it here - someone might use it and adopt or enhance it.
12 + Or not. I don't care ;)
13 +
14 + """
15 +
16 + import os
17 + import ipaddress
18 +
19 + def main():
20 + """main function
21 + """
22 +
23 + ipfile = "/etc/ipset.conf"
24 + blacklist = os.path.expanduser('~') + "/blacklist.txt"
25 +
26 + if os.path.isfile(ipfile) and os.access(ipfile, os.R_OK):
27 + with open("/etc/ipset.conf", "r") as f:
28 + lines=f.readlines()
29 + ips=[]
30 + for line in lines:
31 + if 'add badips' in line:
32 + ips.append(line.split(' ')[2].strip())
33 + else:
34 + print("Could not open ipset file")
35 + exit(1)
36 +
37 + f.close()
38 +
39 + # convert into ipaddress objects
40 + ips = [ipaddress.IPv4Address(_) for _ in ips]
41 + cidrs = list(ipaddress.collapse_addresses(ips))
42 +
43 + with open(blacklist, "w") as file:
44 + file.write('\n'.join(str(i) for i in cidrs))
45 +
46 + file.close()
47 +
48 +
49 + if __name__ == "__main__":
50 + main()
Siguiente Anterior