[英]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.