1、判断是否是一个目录
import os
dir = "/var/www/html/EnjoyCarApi/"
if os.path.isdir(dir):
print('%s is a dir' % dir)
else:
print('%s is not a dir' % dir)
2、计算网段的IP
import IPy
ip = IPy.IP('172.16.0.0/26')
print(ip.len())
for i in ip:
print(i)
3、gitlab脚本,实现自动操作
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)
4、系统内存与磁盘
import psutil
def memissue():
print('内存信息:')
mem = psutil.virtual_memory()
# 单位换算为MB
memtotal = mem.total/1024/1024
memused = mem.used/1024/1024
membaifen = str(mem.used/mem.total*100) + '%'
print('%.2fMB' % memused)
print('%.2fMB' % memtotal)
print(membaifen)
def cuplist():
print('磁盘信息:')
disk = psutil.disk_partitions()
diskuse = psutil.disk_usage('/')
#单位换算为GB
diskused = diskuse.used / 1024 / 1024 / 1024
disktotal = diskuse.total / 1024 / 1024 / 1024
diskbaifen = diskused / disktotal * 100
print('%.2fGB' % diskused)
print('%.2fGB' % disktotal)
print('%.2f' % diskbaifen)
memissue()
print('*******************')
cuplist()
5、解析域名的IP
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])
6、下载阿里云RDS二进制日志
'''
查询阿里云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()
7、将磁盘使用情况写入文件中,并按天保存
import time, os
new_time = time.strftime("%Y-%m-%d")
disk_status = os.popen('df -h').readlines() #readlines
f = open(new_time+'.log', 'w')
f.write('%s\n' % disk_status)
f.flush()
f.close()
/*
read 读取整个文件
readline 读取下一行
readlines 读取整个文件到一个迭代器以供我们遍历(读取到一个list中,以供使用,比较方便)
*/
8、统计nginx每个IP访问量
list = []
f = open('/var/log/httpd/access_log', 'r')
star = f.readlines()
f.close()
for i in star:
ip = i.split()[0]
list.append(ip)
list_num = set(list)
for j in list_num:
num = list.count(j)
print('%s-%s' %(j, num))