我想自动生成一个可以到达特定 IP 地址的 tor 导出节点列表。我在网上搜索了一会儿,从 http://sjoerd-hemminga.com/blog/2012/10/block-tor-exit-nodes-using-iptables/ 中找到了这段代码。
if [[ -z "$1" ]]; then
echo Usage: $0 "<your host's ip>"
exit 1
fi
hostip=$1
for i in $(wget https://check.torproject.org/cgi-bin/TorBulkExitList.py\?ip=$hostip -O- -q |\
grep -E '^[[:digit:]]+(\.[[:digit:]]+){3}$'); do
sudo iptables -A INPUT -s "$i" -j DROP
done
谁能帮助我更好地理解这段代码,因为每次我尝试运行它时,它都会产生错误。
欢迎任何替代答案,但我希望它们是用 Python 编写的。
最佳答案
import os
import re
import sys
import urllib
if len(sys.argv) != 2:
print "Usage {} <your host's ip>".format(sys.argv[0])
sys.exit(1)
hostip = sys.argv[1]
u = urllib.urlopen('https://check.torproject.org/cgi-bin/TorBulkExitList.py?ip=' + hostip)
for ip in u:
ip = ip.strip()
if re.match('\d+(\.\d+){3}$', ip):
#print ip
os.system('sudo iptables -A INPUT -s "{}" -j DROP'.format(ip))
u.close()
关于python - 自动生成 tor 导出节点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16977923/