0%

多线程ping检测IP地址存活

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 15:19
# @Author : Jeff
# @Site :
# @File : multiprocess_check_ping.py
# @Software: PyCharm
'''
使用多进程检测ping,并取值.默认开启4个进程,需要将hosts.txt IP列表文件放入同一目录下,IP列表每行一个,支持域名、IP
'''
import multiprocessing
import re
import sys,os
import commands
import datetime

def pinger(ip):
cmd='ping -c 2 %s' % (ip.strip())
ret = commands.getoutput(cmd)
loss_re=re.compile(r"received, (.*) packet loss")
packet_loss=loss_re.findall(ret)[0]
rtt_re=re.compile(r"rtt min/avg/max/mdev = (.*) ")
rtts=rtt_re.findall(ret)
#rtt.split(["/"])
rtt=rtts[0].split('/')
rtt_min=rtt[0]
rtt_avg=rtt[1]
rtt_max=rtt[2]
print "%s\t\t%s\t\t%s\t\t%s\t\t%s"%(ip,packet_loss,rtt_min,rtt_max,rtt_avg)



if __name__ == "__main__":
if not os.path.exists("hosts.txt") :
print "\033[31mhosts.txt文件不存在,请重试\033[0m"
sys.exit(1)
now=datetime.datetime.now()
file=open('hosts.txt','r')
pool=multiprocessing.Pool(processes=4)
result=[]
print "########%s###########"%now
print "IPADDRSS\t\t\tLOSS\t\tMIN\t\tMAX\t\tAVG"
for i in file.readlines():
if len(i)==1 or i.startswith("#"):
continue
result.append(pool.apply_async(pinger,(i.strip(),)))
pool.close()
pool.join()
阅读全文 »

清除指定redis缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 15:19
# @Author : Jeff
# @Site :
# @File : clean_redis_key.py
# @Software: PyCharm

'''
python3环境运行;
默认连接本地redis,如要远程连接redis,修改host和端口即可如有密码校验未做处理,需要自行修改脚本;
需安装redis库, pip install redis
'''

import redis

#选择连接的数据库
db = input('输入数据库:')
r = redis.Redis(host='127.0.0.1',port=6379,db=0)

#输入要匹配的键名
id = input('请输入要执匹配的字段:')
arg = '*' + id + '*'

n = r.keys(arg)
#查看匹配到键值
for i in n:
print(i.decode('utf-8'))

#确定清除的键名
delid = input('输入要删除的键:')

print('清除缓存 %s 成功' % delid)
阅读全文 »

判断是否是一个目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 15:16
# @Author : Jeff
# @Site :
# @File : is_dir.py
# @Software: PyCharm

import os

dir = "/data/www/html/v6/"
if os.path.isdir(dir):
print('%s is a dir' % dir)
else:
print('%s is not a dir' % dir)
阅读全文 »

统计nginx日志前十ip访问量并以柱状图显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 15:49
# @Author : Jeff
# @Site :
# @File : nginx_ip.py
# @Software: PyCharm

import matplotlib.pyplot as plt
#
nginx_file = 'access.log.20190927'

ip = {}
# 筛选nginx日志文件中的ip
with open(nginx_file) as f:
for i in f.readlines():
s = i.strip().split()[0]
lengh = len(ip.keys())

# 统计每个ip的访问量以字典存储
if s in ip.keys():
ip[s] = ip[s] + 1
else:
ip[s] = 1

#以ip出现的次数排序返回对象为list
ip = sorted(ip.items(), key=lambda e:e[1], reverse=True)

#取列表前十
newip = ip[0:10:1]
tu = dict(newip)

x = []
y = []
for k in tu:
x.append(k)
y.append(tu[k])
plt.title('ip access')
plt.xlabel('ip address')
plt.ylabel('PV')

#x轴项的翻转角度
plt.xticks(rotation=70)

#显示每个柱状图的值
for a,b in zip(x,y):
plt.text(a, b, '%.0f' % b, ha='center', va= 'bottom',fontsize=7)

plt.bar(x,y)
plt.legend()
plt.show()
阅读全文 »

gitlab钩子脚本实现简单自动化操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 17:41
# @Author : Jeff
# @Site :
# @File : gitlabCi.py
# @Software: PyCharm

from flask import Flask,request,render_template,make_response,Response
import json,os,re,requests
import subprocess

app = Flask(__name__)
null = ""
cmd = "/var/www/html/ladmin-devel/"
@app.route('/test',methods=['POST'])
def hello():
json_dict = json.loads(request.data)

name = json_dict['event_name']
ref = json_dict['ref'][11:]
project = json_dict['project']['name']

if name == 'push' and ref == 'master':
os.chdir(cmd)
s = subprocess.getoutput('sudo -u nginx composer install')
return Response(s)
else:
return Response('none')

if __name__ == '__main__':
app.run(host='0.0.0.0',port=8080)
阅读全文 »

解析一组域名的ip地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-20 10:21
# @Author : Jeff
# @Site :
# @File : dnsReloves.py
# @Software: PyCharm

import dns.resolver
from collections import defaultdict
hosts = ['baidu.com','weibo.com']
s = defaultdict(list)
def query(hosts):
for host in hosts:
ip = dns.resolver.query(host,"A")
for i in ip:
s[host].append(i)

return s

for i in query(hosts):

print(i,s[i])
阅读全文 »

下载阿里云RDS二进制日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-12 13:52
# @Author : Jeff
# @Site :
# @File : rds_binlog.py
# @Software: PyCharm

'''
查询阿里云rds binlog日志
'''

import base64,urllib.request
import hashlib
import hmac
import uuid,time,json,wget

class RDS_BINLOG_RELATE(object):

def __init__(self):
#阿里云的id和key
self.access_id = '**********************'
self.access_key = '**********************'

#通过id和key来进行签名
def signed(self):
timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
header = {
'Action': 'DescribeBinlogFiles',
'DBInstanceId': 'rm-wz9azm783q621n9',
'StartTime': '2018-07-11T15:00:00Z',
'EndTime': timestamp,
'Format': 'JSON',
'Version': '2014-08-15',
'AccessKeyId': self.access_id,
'SignatureVersion': '1.0',
'SignatureMethod': 'HMAC-SHA1',
'SignatureNonce': str(uuid.uuid1()),
'TimeStamp': timestamp,

}

#对请求头进行排序
sortedD = sorted(header.items(), key=lambda x: x[0])
url = 'https://rds.aliyuncs.com'
canstring = ''

#将请求参数以#连接
for k, v in sortedD:
canstring += '&' + self.percentEncode(k) + '=' + self.percentEncode(v)

#对请求连接进行阿里云要的编码规则进行编码
stiingToSign = 'GET&%2F&' + self.percentEncode(canstring[1:])

bs = self.access_key + '&'
bs = bytes(bs, encoding='utf8')
stiingToSign = bytes(stiingToSign, encoding='utf8')
h = hmac.new(bs, stiingToSign, hashlib.sha1)
stiingToSign = base64.b64encode(h.digest()).strip()

#将签名加入到请求头
header['Signature'] = stiingToSign

#返回url
url = url + "/?" + urllib.parse.urlencode(header)
return url

#按照规则替换
def percentEncode(self,store):
encodeStr = store
res = urllib.request.quote(encodeStr)
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return str(res)

#筛选出链接下载二进制日志文件
def getBinLog(self):
binlog_url = self.signed()
req = urllib.request.urlopen(binlog_url)
req = req.read().decode('utf8')
res = json.loads(req)

for i in res['Items']['BinLogFile']:
wget.download(i['DownloadLink'])

s = RDS_BINLOG_RELATE()
s.getBinLog()
阅读全文 »

探测Web服务质量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 17:41
# @Author : Jeff
# @Site :
# @File : check_web_url_speed.py
# @Software: PyCharm

# 该脚本可以定位访问web页面的服务质量
# 通过Python下的pycurl模块来实现定位
# 它可以通过调用pycurl提供的方法,来探测Web服务质量
# 比如了解相应的HTTP状态码、请求延时、HTTP头信息、下载速度等

import os
import time
import sys
import pycurl

# 探测目标URL
URL = "http://www.baidu.com"
# 创建一个Curl对象
c = pycurl.Curl()
# 定义请求的URL变量
c.setopt(pycurl.URL, URL)
# 定义请求连接的等待时间
c.setopt(pycurl.CONNECTTIMEOUT, 5)
# 定义请求超时时间
c.setopt(pycurl.TIMEOUT, 5)
# 屏蔽下载进度条
c.setopt(pycurl.FORBID_REUSE, 1)
# 指定HTTP重定向的最大数为1
c.setopt(pycurl.MAXREDIRS, 1)
# 完成交互后强制断开连接,不重用
c.setopt(pycurl.NOPROGRESS, 1)
# 设置保存DNS信息的时间为30秒
c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)
# 创建一个文件对象,以“wb”方式打开,用来存储返回的http头部及页面的内容
indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt", "wb")
# 将返回的HTTP HEADER定向到indexfile文件
c.setopt(pycurl.WRITEHEADER, indexfile)
# 将返回的HTML内容定向到indexfile文件
c.setopt(pycurl.WRITEDATA, indexfile)
# 捕捉Curl.perform请求的提交,如果错误直接报错退出
try:
c.perform()
except Exception,e:
print "连接错误"
indexfile.close()
c.close()
sys.exit()
# DNS解析所消耗的时间
NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME)
# 建立连接所消耗的时间
CONNECT_TIME = c.getinfo(c.CONNECT_TIME)
# 从建立连接到准备传输所消耗的时间
PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME)
# 从建立连接到传输开始消耗的时间
STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)
# 传输结束所消耗的总时间
TOTAL_TIME = c.getinfo(c.TOTAL_TIME)
# 返回HTTP状态码
HTTP_CODE = c.getinfo(c.HTTP_CODE)
# 下载数据包的大小
SIZE_DOWNLOAD = c.getinfo(c.SIZE_DOWNLOAD)
# HTTP头部大小
HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
# 平均下载速度
SPEED_DOWNLOAD = c.getinfo(c.SPEED_DOWNLOAD)
print "HTTP状态码:%d" %HTTP_CODE
print "DNS解析时间:%.2f ms"%(NAMELOOKUP_TIME*1000)
print "建立连接时间:%.2f ms" %(CONNECT_TIME*1000)
print "准备传输时间:%.2f ms" %(PRETRANSFER_TIME*1000)
print "传输开始时间:%.2f ms" %(STARTTRANSFER_TIME*1000)
print "传输结束总时间:%.2f ms" %(TOTAL_TIME*1000)
print "下载数据包大小:%d bytes/s" %(SIZE_DOWNLOAD)
print "HTTP头部大小:%d byte" %(HEADER_SIZE)
print "平均下载速度:%d bytes/s" %(SPEED_DOWNLOAD)
indexfile.close()
c.close()
"""
HTTP状态码:%d
DNS解析时间:%.2f ms
建立连接时间:%.2f ms
准备传输时间:%.2f ms
传输开始时间:%.2f ms
传输结束总时间:%.2f ms
下载数据包大小:%d bytes/s
HTTP头部大小:%d byte
平均下载速度:%d bytes/s
""" %(HTTP_CODE, NAMELOOKUP_TIME*1000, CONNECT_TIME*1000, PRETRANSFER_TIME*1000, STARTTRANSFER_TIME*1000, TOTAL_TIME*1000, SIZE_DOWNLOAD,HEADER_SIZE, SPEED_DOWNLOAD)
阅读全文 »

发送邮件告警

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-10-28 17:41
# @Author : Jeff
# @Site :
# @File : send_mail.py
# @Software: PyCharm

import smtplib
from email.mime.text import MIMEText
'''
发送邮件函数,默认使用163smtp
:param mail_host: 邮箱服务器,16邮箱host: smtp.163.com
:param port: 端口号,163邮箱的默认端口是 25
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱密码(不是邮箱的登录密码,是邮箱的授权码)
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:return:
'''
def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
msg = MIMEText(content) # 邮件内容
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
smtp = smtplib.SMTP(mail_host, port=port) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(username, passwd) # 登录发送者的邮箱账号,密码
# 参数分别是 发送者,接收者,第三个是把上面的发送邮件的 内容变成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 发送完毕后退出smtp
print('email send success.')
if __name__ == '__main__':
email_user = 'xxxx@163.com' # 发送者账号
email_pwd = 'xxxxx' # 发送者密码,授权码
maillist = 'xxxx@qq.com'
title = '测试邮件标题'
content = '这里是邮件内容'
send_mail(email_user, email_pwd, maillist, title, content)
阅读全文 »