动态获取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 ...
随机推荐
- 1、纯python编写学生信息管理系统
1.效果图 2.python code: class studentSys(object): ''' _init_(self) 被称为类的构造函数或初始化方法, self 代表类的实例,self 在定 ...
- axios中请求传值方式
日常开发中与后端联调,可能需要的数据不同,所传值也有所不同 1.如果是data方式,设置请求头为:并且直接返回data就可以 raw axios.defaults.headers['Content- ...
- Django3.0.2学习踩坑记
配置文件settings.py相关: 新增app INSTALLED_APPS = [ 'polls.apps.PollsConfig', # 这个是新增的APP 'django.contrib.ad ...
- AVR单片机教程——小结
本文隶属于AVR单片机教程系列. 第一期挺让我失望的,是我太菜,没有把想讲的都讲出来.经常写了很多,然后一点一点删掉,最后就没多少了. 而且感觉难度不合适,处于很尴尬的位置.讲得简单,难的丢给库, ...
- python 面向对象-初识
一.分类 1.面向过程 2.面向函数 3.面向对象 二.类和对象 1.宏关 类是抽象的,对象是具体的 2.实例化 类->对象 3.类的作用 1)实例化成对象 实例化的过程,创建self对象,调用 ...
- SpringSecurity 默认表单登录页展示流程源码
SpringSecurity 默认表单登录页展示流程源码 本篇主要讲解 SpringSecurity提供的默认表单登录页 它是如何展示的的流程, 涉及 1.FilterSecurityIntercep ...
- [bzoj4567] [loj#2012] [SCOI2016] 背单词
Description \(Lweb\) 面对如山的英语单词,陷入了深深的沉思,「我怎么样才能快点学完,然后去玩三国杀呢?」.这时候睿智的凤老师从远处飘来,他送给了 \(Lweb\) 一本计划册和一大 ...
- 每天翻译一点点: WPF Application Framework (WAF)
ps:http://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&referringTitle=Document ...
- 基于快排思想的第(前)k大(小)
算法思路就是根据快排的partition,先随机选择一个分隔元素(或a[0]),将数组分为[小于a[p]的元素] a[p] [大于a[p]的元素],如果这时候n-p+1等于k的话,a[p]就是所求的第 ...
- JS中for...in循环陷阱及遍历数组的方式对比
JavaScript中有很多遍历数组的方式,比较常见的是for(var i=0;i<arr.length;i++){},以及for...in...循环等,这些遍历都有各自的优缺点,下面来看看各种 ...