subl.py
· 1.2 KiB · Python
原始文件
#!/usr/bin/env python3
""" Sort the blacklist ip list file into CIDR networks
Author: Dominic Reich “OE7DRT”
<quick.hat4396@qtztsjosmprqmgtunjyf.com>
Usage: Run the program, paths are fixed (specified below)
Unfinished and aborted - I don't need something like this any more.
So i published it here - someone might use it and adopt or enhance it.
Or not. I don't care ;)
"""
import os
import ipaddress
def main():
"""main function
"""
ipfile = "/etc/ipset.conf"
blacklist = os.path.expanduser('~') + "/blacklist.txt"
if os.path.isfile(ipfile) and os.access(ipfile, os.R_OK):
with open("/etc/ipset.conf", "r") as f:
lines=f.readlines()
ips=[]
for line in lines:
if 'add badips' in line:
ips.append(line.split(' ')[2].strip())
else:
print("Could not open ipset file")
exit(1)
f.close()
# convert into ipaddress objects
ips = [ipaddress.IPv4Address(_) for _ in ips]
cidrs = list(ipaddress.collapse_addresses(ips))
with open(blacklist, "w") as file:
file.write('\n'.join(str(i) for i in cidrs))
file.close()
if __name__ == "__main__":
main()
| 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() |