最后活跃于 2 weeks ago

修订 45b24f878a161aacb7636d3143c9d2fd2e7f800f

subl.py 原始文件
1#!/usr/bin/env python3
2
3""" Sort the blacklist ip list file into CIDR networks
4
5Author: Dominic Reich “OE7DRT”
6 <quick.hat4396@qtztsjosmprqmgtunjyf.com>
7
8Usage: 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
16import os
17import ipaddress
18
19def 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
49if __name__ == "__main__":
50 main()