动态获取bind dns日志IP脚本
#!/usr/bin/env python
#_*_coding:utf-8_*_
'''
python deny_dns_allip.py your_filelog_name
动态获取dns日志的IP地址,把不满足条件的都drop掉
此脚本修改后可以动态拒绝任何日志内的IP
相当于 tail -f filename | awk -F "你的条件" ,然后 把结果输送到iptables -I INPUT -s %s -j DROP
'''
from sys import argv
import collections
import time,os,re
o = open(argv[1], 'r')
print(''.join(collections.deque(o, 5)).strip('\n')) # last 5 lines
o.seek(0,2) # jump to last line ip_list = []
while 1:
line = o.readline()
if not line:
time.sleep(0.1)
continue
ret = line.strip('\n')
try:
#正则的条件,根据该字段获取该字段的IP
ip = str(ret.split("query: hoffmeister.be IN ANY +E")[0])
ipstr = re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',ip).group()
#排除部分IP,排除172网段124网段211网段,根据需要自己定义
exclude_ips = str(ipstr.split(".")[0])
allow_ips = ['','','']
#
#print("ip_list:",ip_list)
if ipstr not in ip_list and exclude_ips not in allow_ips:
ip_list.append(ipstr)
#取最新的IP,然后drop掉
print("======>>i:",ip_list[-1])
os.system("iptables -I INPUT -s %s -j DROP"%(ip_list[-1]))
print("iptables -I INPUT -s %s -j DROP"%(ip_list[-1]))
else:
pass
except:
b=None
加强版
动态获取日志,然后调用淘宝ip查询的API 智能判断 来源IP属于什么地方,当是国外的IP时,直接干掉。
解决了部分用户 使用手机4G测试时的被意外干掉的情况。
# -*- coding: utf-8 -*-
import requests,os def checkip(ip): URL = 'http://ip.taobao.com/service/getIpInfo.php'
try:
r = requests.get(URL, params=ip, timeout=2)
except requests.RequestException as e:
#pass
print(e)
else:
json_data = r.json()
if json_data[u'code'] == 0:
#print "ipvalues",type(ip.values())
ret = json_data[u'data'][u'country'].encode('utf-8') + str(ip.values())
#print ret
country = json_data[u'data'][u'country'].encode('utf-8')
if country != '中国':
ret_ip = ip.values()[0]
print "---------------country and ip",country, ret_ip
os.system("iptables -I INPUT -s %s -j DROP"%(ret_ip))
print("iptables -I INPUT -s %s -j DROP"%(ret_ip))
else:
print"----china",ip.values()[0]
#return json_data[u'data'][u'country'].encode('utf-8') + str(ip.values())
#print '所在地区: ' + json_data[u'data'][u'area'].encode('utf-8')
#print '所在省份: ' + json_data[u'data'][u'region'].encode('utf-8')
#print '所在城市: ' + json_data[u'data'][u'city'].encode('utf-8')
#print '所属运营商:' + json_data[u'data'][u'isp'].encode('utf-8')
else:
#pass
print '查询失败,请稍后再试!' #ip= {'ip':'67.177.203.45'}
#checkip(ip)
#!/usr/bin/env python
#_*_coding:utf-8_*_ from sys import argv
import collections
import time,os,re
import ip_check o = open(argv[1], 'r')
print(''.join(collections.deque(o, 5)).strip('\n')) # last 5 lines
o.seek(0,2) # jump to last line ip_list = []
while 1:
line = o.readline()
if not line:
time.sleep(2)
continue
ret = line.strip('\n')
try:
ip = str(ret.split("query: hoffmeister.be IN ANY +E")[0])
ipstr = re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',ip).group()
#
exclude_ips = str(ipstr.split(".")[0])
allow_ips = ['','','']
#
#print("ip_list:",ip_list)
if ipstr not in ip_list and exclude_ips not in allow_ips:
ip_list.append(ipstr)
print("======>>i:",ip_list[-1])
#f = open('ip.txt','a+')
#f.write(ip_list[-1]+'\n')
#f.close()
check_ips = {'ip':ip_list[-1]}
ip_check.checkip(check_ips)
else:
pass
except:
b=None
bind dns日志配置 添加如下行。有点诡异 日志的路径并非 根下的var 而是运行bind的根下的var
[root@localhost var]# ls
log named run tmp
[root@localhost var]# cd log/
[root@localhost log]# ls
den_txt_ip.py deny_dns_ip.py ip-check.txt query.log query_log. query_log. query_log. query_log. query_log. query_log. test.log
deny_dns_allip.py ip_check.py ip.txt query_log. query_log. query_log. query_log. query_log. query_log. query_log. test.py
deny_dns_allip.py.bak ip_check.pyc query_log query_log. query_log. query_log. query_log. query_log. query_log. read_ip.py
[root@localhost log]# pwd
/var/named/chroot/var/log
[root@localhost log]# ps -ef |grep "named"
root Jun22 ? :: named -d
named : ? :: /usr/sbin/named -u named -t /var/named/chroot
root : pts/ :: grep named
root Jun22 ? :: named -d
logging {
/*指定服务器日志记录的内容和日志信息来源*/
channel "default_syslog" {
syslog daemon; /* 发送给syslog 的daemon facility */
severity info; /* 只发送此优先级和更高优先级的信息 */
};
channel default_debug {
file "data/named.run"; /* 写入工作目录下的named.run 文件。注意:如果服务器用-f 参数启动,则"named.run"会被stderr 所替换。*/
severity dynamic; /* 按照服务器当前的debug 级别记录日志 */
};
channel xfer_in_log {
file "/var/log/named/xfer_in_log" versions size 10m;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel xfer_out_log {
file "/var/log/named/xfer_out_log" versions size 10m;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel notify_log {
file "/var/log/named/notify_log" versions size 10m;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel general_log {
file "/var/log/named/general_log" versions size 100m;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel default_log {
file "/var/log/named/default_log" versions size 100m;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel update_log {
file "/var/log/named/update_log" versions size 10m;
severity info;
print-category yes;
print-severity yes;
print-time yes;
};
channel query_log {
file "/var/log/query_log" versions size 100m;
severity info;
print-category no;
print-severity no;
print-time yes;
};
category queries { query_log; };
category default { default_log; };
category general { general_log; };
category xfer-in { xfer_in_log; };
category xfer-out { xfer_out_log; };
category notify { notify_log; };
category update { update_log; };
};
动态获取bind dns日志IP脚本的更多相关文章
- 使用WIFI网卡 dhcp动态获取IP
前面几篇博客中,wifi网卡的ip都是手工设置的,本篇博客将来移植dhcp,使得wifi网卡可以动态的获取ip.路由等信息. 那我们去哪里下载dhcp源码呢?在pc机上执行dh +tab键,看一下有哪 ...
- VMware虚拟机在仅主机模式下的网卡无法动态获取IP
自己在VMware虚拟机中开启一台主机的时候,发现比以往的开机速度慢了好多,起初不以为然,直到用Xshell通过ssh远程连接eth1的ip地址才发现连接失败(这个ip是之前eth1正常的时候获取的i ...
- nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容
1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...
- C# — 动态获取本地IP地址及可用端口
1.在VS中动态获取本地IP地址,代码如下: 2.获取本机的可用端口以及已使用的端口:
- python3脚本获取本机公网ip
python脚本获取本机公网ip 1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.代码实现 import requests import re r ...
- scrapy实战9动态设置ip代理从数据库中随机获取一个可用的ip:
在目录下创建tools(python package) 在tools中创建crawl_xici_ip.py文件写入代码如下: #coding=utf-8 import requests from sc ...
- 日志系统实战(二)-AOP动态获取运行时数据
介绍 这篇距上一篇已经拖3个月之久了,批评自己下. 通过上篇介绍了解如何利用mono反射代码,可以拿出编译好的静态数据.例如方法参数信息之类的. 但实际情况是往往需要的是运行时的数据,就是用户输入等外 ...
- Ngxin 开启CDN 日志获取不了真实IP的解决办法。
nginx配置里面在http{ 后加入如下两行代码即可: set_real_ip_from 0.0.0.0/0;real_ip_header X-Forwarded-For; 重启nginx生效. 注 ...
- Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP
由于电脑设置静态IP经常出现链接不上网络,动态IP又非常不方便,故有了这个想法并实现 原理: Linux,包含PC机器,树莓派等,通过shell 自动获取报告本机IP | 通过 Mutt+Msmtp ...
随机推荐
- 最短路SPFA模板
// // dijkstra妯℃澘.cpp // algorithm // // Created by david.xu on 2018/8/6. // Copyright 漏 2018骞?david ...
- Java入门 - 面向对象 - 06.接口
原文地址:http://www.work100.net/training/java-interface.html 更多教程:光束云 - 免费课程 接口 序号 文内章节 视频 1 概述 2 接口的声明 ...
- Codeforces940掉分记
掉分经过 难得这次时间比较好,下午17:35开始. 本来还很高兴,心想这回肯定不会犯困,没准排名能再上升一些呢,,可惜事与愿违-- 上来a题,光看懂题就花了一些时间. 然后开始写,结果第一遍CE,第二 ...
- 部署Maven项目到tomcat报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi
Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderL 严重: Error config ...
- 钝化 会钝化 订单审批流程 码一会er
先放一张订单审批流程图.预则立嘛
- web api 的 安全 认证问题 , 对外开放 的 时候 需要考虑到安全的问题
关于 OWIN OAuth , web api的认证,全局验证, 安全方面的验证 有必要 去 自己捣鼓一下.
- 19_07_8校内训练[sort]
题意 一个排列,每次选一个子序列按顺序放在开头,要求变成升序的操作次数不超过17次,给出方案.n<=1E5. 思考 对于ai=aj-1且i<j的数字,一定要保持其相对顺序.可以根据这个关系 ...
- 必须掌握的50条Linux基础操作命令
说明: 根据笔者平时使用情况,并结合参考一些其他资料,精心整理出以下必须掌握的50条Linux基础命令. 命令: 1. ssh 登录到远程主机 $ ssh -l jsmith remotehost.e ...
- Postwoman-接口测试工具
地址是:https://postwoman.io/ ,不过只能使用它们自己提供的测试接口,如果你调试自己的API接口的话,你需要自己部署一套代码. 自己搭建一套Postwomen环境的话,只需要安装了 ...
- learn more ,study less(三):超越整体性学习
高效率的学生 成为一名高效率学生或是自学者需 要掌握减少花在书本上时间的艺术,我上学时,除了全日制的上课学习,业余时间经营一家 企业,每周写大约 7000 字,健身以及主持一家演讲俱乐部,尽管如此,我 ...