配置环境:ubuntu16.04/apache2
Ubuntu16.04服务器环境下:
一、建立脚本文件:
sudo nano getips.py
import ftplib import ftplib import sys from math import log from os import path apnicip = ftplib.FTP('ftp.apnic.net') apnicip.login() apnicip.cwd('/public/apnic/stats/apnic') ipfile = open('delegated-apnic-latest', 'w') print('start retriving!') apnicip.retrbinary('RETR delegated-apnic-latest', ipfile.write) ipfile.close() ipfinal = open('ip.txt', 'w') ipfile = open('delegated-apnic-latest', 'r') ipori = ipfile.readlines() print('Start grabbing!') for ips in ipori: try: if ips.split('|')[2] == 'ipv4': pass else: continue except IndexError: continue if ips.split('|')[1] == 'JP': ip = ips.split('|')[3] netmask = str(int(32 - log(int(ips.split('|')[4]), 2))) ipfinal.writelines('allow from ' + ip + '/' + netmask + '\n') for ips in ipori: try: if ips.split('|')[2] == 'ipv6': pass else: continue except IndexError: continue if ips.split('|')[1] == 'JP': ip = ips.split('|')[3] netmask = ips.split('|')[4] ipfinal.writelines('allow from ' + ip + '/' + netmask + '\n') print('Grab complete!') ipfinal.close()
直接在ipfinal.writelines()生成需要的内容行。
二、执行脚本文件:
python getips.py
在目录下获得一个 ip.txt,里面已经有了相应国家的访问IP列表
把里面的内容复制到.htaccess里好了。
自动获取IP的脚本来自:https://github.com/hanbang-wang/APNIC-IPs