python通过日志分析加入黑名单
#coding=utf-8 import os,time while True: ip_list = [] src_log = '/opt/lampp/logs/access_log' new_log = '/opt/lampp/logs/access_log_%s'%time.strftime('%Y%m%d%H%M')#分割每分钟日志 os.system('cat {s_log} > {n_log} && > {s_log}'.format(s_log=src_log,n_log=new_log) ) with open(new_log) as fr: for line in fr: ip = line.split('-')[0].strip() ip_list.append(ip) for ip in ip_list: if ip_list.count(ip) > 5: os.system('iptables -I INPUT 1 -p tcp -s %s -j DROP'%ip) for i in range(ip_list.count(ip)): ip_list.remove(ip) time.sleep(60)