我想使用this CSV file拦截列表中的所有I。CSV文件中的条目是IP范围。如何从文本中分离IP范围,并将这些IP范围添加到iptables中,并使用丢弃所有与这些IP的连接的规则。
顺便说一句,我认为这更适合用于大ip范围,http://ipset.netfilter.org/index.html
发布于 2016-04-08 17:12:31
为此,您需要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])发布于 2016-04-08 17:07:17
也许可以看看https://docs.python.org/3/library/ipaddress.html来操纵你的ip地址。ipaddress.summarize_address_range(first,last)可以很好地生成iptables规则。
https://stackoverflow.com/questions/36495229
复制相似问题