[英]How to use Python to put a CSV list of ips in iptables / IP Sets?
我想使用此CSV文件来阻止列表中的所有IP。 CSV文件中的条目是IP范围。 如何将IP范围与文本分开,并使用规则删除与这些IP的所有连接,将这些IP范围添加到iptables
。
顺便说一句,我认为最好将其用于较大的IP范围, http://ipset.netfilter.org/index.html
您将需要python netaddr模块
import netaddr
with open('ipranges.txt','r') as f:
for line in f:
startip,endip=line.split(',')[:2]
print 'iptables -I INPUT -s {} -j DROP'.format(netaddr.iprange_to_cidrs(startip, endip)[0])
也许看看https://docs.python.org/3/library/ipaddress.html来操纵您的IP地址。 ipaddress.summarize_address_range(first,last)可以很好地生成iptables规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.