#!/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脚本的更多相关文章

  1. 使用WIFI网卡 dhcp动态获取IP

    前面几篇博客中,wifi网卡的ip都是手工设置的,本篇博客将来移植dhcp,使得wifi网卡可以动态的获取ip.路由等信息. 那我们去哪里下载dhcp源码呢?在pc机上执行dh +tab键,看一下有哪 ...

  2. VMware虚拟机在仅主机模式下的网卡无法动态获取IP

    自己在VMware虚拟机中开启一台主机的时候,发现比以往的开机速度慢了好多,起初不以为然,直到用Xshell通过ssh远程连接eth1的ip地址才发现连接失败(这个ip是之前eth1正常的时候获取的i ...

  3. nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容

    1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...

  4. C# — 动态获取本地IP地址及可用端口

    1.在VS中动态获取本地IP地址,代码如下: 2.获取本机的可用端口以及已使用的端口:

  5. python3脚本获取本机公网ip

    python脚本获取本机公网ip   1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.代码实现 import requests import re r ...

  6. scrapy实战9动态设置ip代理从数据库中随机获取一个可用的ip:

    在目录下创建tools(python package) 在tools中创建crawl_xici_ip.py文件写入代码如下: #coding=utf-8 import requests from sc ...

  7. 日志系统实战(二)-AOP动态获取运行时数据

    介绍 这篇距上一篇已经拖3个月之久了,批评自己下. 通过上篇介绍了解如何利用mono反射代码,可以拿出编译好的静态数据.例如方法参数信息之类的. 但实际情况是往往需要的是运行时的数据,就是用户输入等外 ...

  8. Ngxin 开启CDN 日志获取不了真实IP的解决办法。

    nginx配置里面在http{ 后加入如下两行代码即可: set_real_ip_from 0.0.0.0/0;real_ip_header X-Forwarded-For; 重启nginx生效. 注 ...

  9. Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP

    由于电脑设置静态IP经常出现链接不上网络,动态IP又非常不方便,故有了这个想法并实现 原理: Linux,包含PC机器,树莓派等,通过shell 自动获取报告本机IP  | 通过 Mutt+Msmtp ...

随机推荐

  1. Spring Boot 2.X(十九):集成 mybatis-plus 高效开发

    前言 之前介绍了 SpringBoot 整合 Mybatis 实现数据库的增删改查操作,分别给出了 xml 和注解两种实现 mapper 接口的方式:虽然注解方式干掉了 xml 文件,但是使用起来并不 ...

  2. Spring学习记录1——IoC容器

    IoC容器 1.1  IoC概述 Ioc(Inverse of Control,控制反转)是Spring容器的内核.对于软件来说,即某一接口具体实现类的选择控制权从调用类中移除,转交给第三方决定,即由 ...

  3. 序列化之二(将"\/Date(942289871000)\/"格式的时间替换成"yyyy-MM-dd HH:mm:ss"格式)

    序列化就是一种用来处理对象流的机制.所谓对象流也就是将对象的内容进行流化,流的概念这里不用多说(就是I/O).我们可以对流化后的对象进行读写 操作,也可将流化后的对象传输于网络之间(注:要想将对象传输 ...

  4. 使用Razor 使用Razor表达式处理命名空间 精通ASP-NET-MVC-5-弗瑞曼

  5. 双指针,BFS与图论(一)

    (一)双指针 1.日志统计 小明维护着一个程序员论坛.现在他收集了一份”点赞”日志,日志共有 N 行. 其中每一行的格式是: ts id 表示在 ts 时刻编号 id 的帖子收到一个”赞”. 现在小明 ...

  6. GP工作室—Alpha版本发布1

    目录 GP工作室-Alpha版本发布1 一.简介 1.1作业要求 1.2团队成员 二.软件下载安装说明 五.项目总结 @(Gold Point团队の项目计划) GP工作室-Alpha版本发布1 一.简 ...

  7. 创建dynamics CRM client-side (七) - 用JS 来控制Auto-Save

    在我们的system setting里面, 我们可以设置打开/关闭 auto save的功能. 我们可以用js来控制auto-save this.formOnSave = function (exec ...

  8. 基于云开发开发 Web 应用(一):项目介绍 & 初始化

    基于云开发开发 Web 应用(一):项目介绍 & 初始化 背景描述 Linux 中国曾在过去的 1 - 2 年内长期运行了一个 TL;DR 的中国版.不过当时做的版本是小程序的版本,一直以来, ...

  9. [校内训练19_09_10]sort

    题意 给一个非负整数序列,每次问能否异或上一个正整数使得所有的数单调不减.如果能,输出最小的x,否则输出-1.单点修改.多测.要求最多一个log. 思考 只要考虑相邻的两个数.找到这两个数最高的不同的 ...

  10. Tornadofx学习笔记(2)——FxRecyclerView控件的打造

    Tornadofx是基于javafx的一个kotlin框架,用来写些电脑版的小程序 基于Scroll Pane控件,仿造Android中的RecyclerView,实现的一款tornadofx的控件 ...