#!/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:#正则的条件,根据该字段获取该字段的IPip = 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 = ['172','124','211']#
        #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:passexcept:b=None

加强版

动态获取日志,然后调用淘宝ip查询的API 智能判断 来源IP属于什么地方,当是国外的IP时,直接干掉。

解决了部分用户 使用手机4G测试时的被意外干掉的情况。

# -*- coding: utf-8 -*-
import requests,osdef checkip(ip):URL = 'http://ip.taobao.com/service/getIpInfo.php'try:r = requests.get(URL, params=ip, timeout=2)except requests.RequestException as e:#passprint(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 retcountry = json_data[u'data'][u'country'].encode('utf-8')if country != '中国':ret_ip = ip.values()[0]print "---------------country and ip",country, ret_ipos.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:#passprint '查询失败,请稍后再试!'#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_checko = 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 = ['172','124','211']#
        #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:passexcept: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.10  query_log.13  query_log.16  query_log.2  query_log.5  query_log.8  test.log
deny_dns_allip.py      ip_check.py     ip.txt        query_log.0  query_log.11  query_log.14  query_log.17  query_log.3  query_log.6  query_log.9  test.py
deny_dns_allip.py.bak  ip_check.pyc    query_log     query_log.1  query_log.12  query_log.15  query_log.18  query_log.4  query_log.7  read_ip.py
[root@localhost log]# pwd
/var/named/chroot/var/log
[root@localhost log]# ps -ef |grep "named"
root       518     1  0 Jun22 ?        00:00:04 named -d 1
named     4400     1  2 11:20 ?        00:06:47 /usr/sbin/named -u named -t /var/named/chroot
root     29940 26031  0 15:47 pts/8    00:00:00 grep named
root     32069     1  0 Jun22 ?        00:00:01 named -d 1

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 100 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 100 size 10m;  severity info;  print-category yes;  print-severity yes;  print-time yes;  };  channel notify_log {  file "/var/log/named/notify_log" versions 100 size 10m;  severity info;  print-category yes;  print-severity yes;  print-time yes;  };  channel general_log {  file "/var/log/named/general_log" versions 400 size 100m;  severity info;  print-category yes;  print-severity yes;  print-time yes;  };  channel default_log {  file "/var/log/named/default_log" versions 400 size 100m;  severity info;  print-category yes;  print-severity yes;  print-time yes;  };  channel update_log {  file "/var/log/named/update_log" versions 100 size 10m;  severity info;  print-category yes;  print-severity yes;  print-time yes;  };  channel query_log {  file "/var/log/query_log" versions 1024 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; };
};  

转载于:https://www.cnblogs.com/dribs/p/7068675.html

动态获取bind dns日志IP脚本相关推荐

  1. Windows获取本机公网ip脚本

    新建txt,重命名为ip.bat 再将下列文本,贴到ip.bat里 @echo offecho Public IP: curl ifconfig.me echo.pause 双击ip.bat,可得本机 ...

  2. [原创]bind DNS IP列表的精确获取

    [原创]bind DNS IP列表的精确获取 [原创]bind DNS IP列表的精确获取 - LSX - 博客园 [原创]bind DNS IP列表的精确获取 Posted on 2012-02-1 ...

  3. nginx 日志获取不到远程访问ip问题解决

    公司有一个应用,后端web用的是nginx,nginx 的所有请求都是通过前端的代理转发过来的,所有在日志格式里面 获取远程ip的变量用的是 $http_x_forwarded_for ,本来用的好好 ...

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

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

  5. Android通过反射EthernetManager Api设置以太网为静态IP地址或者动态获取IP

    一.对于一些工控行业的Android主板一般都会扩展个以太网接口,也就是可以插网线进行上网:然后在系统设置页面有个以太网菜单可以进行IP.网关等设置,如下: 二.那怎么通过代码去设置呢? 1.Andr ...

  6. 思科dhcp服务器动态获取ip地址

    项目要求: 某公司共有网管中心.行政部.技术部.三个部门,分别处在一栋大楼中的两个楼层,为了保证公司内部主机始终能够连接Internet,采用双向冗余设计,分别使用路由器R1与路由器R2连接中国电信和 ...

  7. virtualbox vm linux 动态获取ip

    修改动态获取ip命令: cd /etc/network/ 修改文件文件中 vi interfaces auto eth0 iface eth0 inet dhcp

  8. 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )

    文章目录 一.基础示例 二.根据字符串动态获取 MetaClass 中的方法 二.使用 evaluate 执行字符串形式的 Groovy 脚本 二.完整代码示例 一.基础示例 定义类 Student ...

  9. centos7安装dhcp服务器并由客户端动态获取IP地址

    主机架构: 角色 ip地址 网关 dhcp服务器 192.168.8.81 192.168.8.1 客户机1 动态获取 192.168.8.81 客户机2 192.168.8.99 192.168.8 ...

最新文章

  1. Markdown 图片助手-MarkdownPicPicker
  2. echo,printr,print_r之间的区别
  3. 给thinkphp3.2用上composer
  4. Python基础--线程
  5. 后缀数组DC3算法实现
  6. 【微机原理与接口技术】多功能可编程芯片 与 多功能电饭煲
  7. 自己动手,开发项目辅助工具
  8. 阿里云搭建图片服务器,图片资源服务器搭建
  9. service下载任务
  10. 简单的三层框架以及使用dbutils进行数据库操作(入门)
  11. 测试压缩ASP.NET中的ViewState
  12. MTK TouchPanel 驱动框架
  13. 中国地图分省设色地图竖版高清矢量cdr、pdf格式2020年
  14. 西瓜视频4K修复技术还原经典,为内容创新打开新思路
  15. 神经网络模型的工作原理,神经网络模型数据处理
  16. 1114 Family Property (25 point(s)) PAT甲级
  17. 全栈学习的知识点梳理(一)
  18. 安卓AndroidStudio设计计算器实现简单的计算
  19. MacBook安装Windows系统后耗电快的原因在这里
  20. Linux的使用和命令的集合

热门文章

  1. spring——Spring自动装配——示例
  2. 芯云融合,全栈可信:曙光超融合一体机重磅发布
  3. python期中考试试卷分析_期中考试考试试卷分析及反思
  4. VP8 的败笔 VS H264
  5. 树莓派4B安装系统,配置远程连接和WiFi,更新源,更新中文支持,基本Linux命令,用Python输出hello和“你好,世界”
  6. 10 位 IT 界女性精英
  7. 计算机系统基础(第一章习题)
  8. 每日安全简讯20160715
  9. 算法工程师福利:超实用技术路线图
  10. 面料防静电测试方法指南