动态获取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 ...
随机推荐
- 什么是aPaas?aPaas与低代码又是如何促进应用程序开发现代化的?
从软件即服务(SaaS)到基础设施即服务(IaaS),云计算的兴起使“一切皆服务”(XaaS)模型得以泛滥,而aPaaS可能是这些模型中最鲜为人知的模型.随着aPaaS市场预计将从2018年的近90亿 ...
- 如何用rflask快速初始化Flask Restful项目
如何用rflask快速初始化Flask Restful项目 说明 多啰嗦两句 我们在创建flask项目的时候,使用pycharm创建出来的项目比较简陋,而且随着项目的功能完善,项目目录结构会比较多,多 ...
- Ubuntu中部署Django项目的配置与链接MySQL
Django的简介 MVT模式的介绍创建项目的虚拟环境 本次使用的是pip安装 一.更新 sudo apt update 二.安装pip sudo apt install python3-pip 三. ...
- 使用luabind绑定box2d的lua接口
最近在使用luabind绑定box2d的lua接口,发现不少问题.写在这里与大家分享. 1. body,fixture,joint的userdata.box2d的userdata的数据类型是void* ...
- linux C++类中成员变量和函数的使用
1.undefined reference to XXX 问题原因 1)XXX所在的so库等未指定 2)XXX在类中实现的时候没有加上类::函数的格式 2. was not declared in t ...
- epel-release的卸载重装
1.yum remove epel-release 2.清空epel目录:rm -rf /var/cache/yum/x86_64/6/epel/ 3.安装,yum install epel-rel ...
- crtmpserver服务器的搭建
https://blog.csdn.net/wutong_login/article/details/7612477 https://www.cnblogs.com/wangqiguo/p/60145 ...
- ios---剪裁圆形图片方法
剪裁圆形图片 - (instancetype)xmg_circleImage { // 1.开启图形上下文 // 比例因素:当前点与像素比例 UIGraphicsBeginImageContextWi ...
- SpringCloud与微服务Ⅷ --- Hystrix断路器
复杂的分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 服务雪崩 多个微服务之间调用的时候,假设微服务调用服务B和微服务C,微服务B和微服务C又调用其他服务,这就是 ...
- DRF框架之Serializer序列化器的反序列化操作
昨天,我们完成了Serializer序列化器的反序列化操作,那么今天我们就来学习Serializer序列化器的最后一点知识,反序列化操作. 首先,我们定要明确什么是反序列化操作? 反序列化操作:JOS ...