python常用运维脚本实例-Python运维常用脚本

判断是否是一个目录

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-18 15:16

# @Author : opsonly

# @Site :

# @File : opsUse.py

# @Software: PyCharm

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)

复制代码

系统内存与磁盘检测

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-17 17:16

# @Author : opsonly

# @Site :

# @File : systemissue.py

# @Software: PyCharm

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()

复制代码

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

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-18 15:49

# @Author : opsonly

# @Site :

# @File : nginx_ip.py

# @Software: PyCharm

import matplotlib.pyplot as plt

#

nginx_file = "nginx2018-12-18_07:45:26’

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()

复制代码

查看网段里有多少ip地址

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-18 15:31

# @Author : opsonly

# @Site :

# @File : ipTest.py

# @Software: PyCharm

import IPy

ip = IPy.IP("172.16.0.0/26’)

print(ip.len())

for i in ip:

print(i)

复制代码

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

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-18 17:41

# @Author : opsonly

# @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地址

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-20 10:21

# @Author : opsonly

# @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])

复制代码

清除指定redis缓存

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-20 15:19

# @Author : opsonly

# @Site :

# @File : redisdel.py

# @Software: PyCharm

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)

复制代码

下载阿里云RDS二进制日志

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @Time : 2018-12-12 13:52

# @Author : opsonly

# @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-sfasffsdaf’,

"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()

作者:opsonly

链接:https://juejin.im/post/5c20bb065188257dc54ae8e2

来源:掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python脚本运维中,中间件是一种常用的技术,用于处理请求和响应之间的逻辑。它可以在请求到达目标服务器之前或响应返回给客户端之前执行一些操作。下面是一个简单的示例,演示了如何使用Python脚本来实现中间件功能: ```python # 定义一个中间件类 class Middleware: def __init__(self, app): self.app = app def __call__(self, environ, start_response): # 在请求到达目标服务器之前执行的操作 # 可以在这里进行身份验证、日志记录等操作 # 调用下一个中间件或目标应用程序 response = self.app(environ, start_response) # 在响应返回给客户端之前执行的操作 # 可以在这里进行响应处理、添加头部信息等操作 return response # 定义一个简单的应用程序 def application(environ, start_response): status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) return [b'Hello, World!'] # 创建中间件实例并将应用程序作为参数传递 app = Middleware(application) # 启动服务器并监听端口 # 这里使用的是Python内置的WSGI服务器 from wsgiref.simple_server import make_server server = make_server('localhost', 8000, app) server.serve_forever() ``` 这个示例中,我们定义了一个`Middleware`类,它接受一个应用程序作为参数,并在`__call__`方法中实现了中间件的逻辑。在`__call__`方法中,我们可以在请求到达目标服务器之前执行一些操作,然后调用下一个中间件或目标应用程序。在响应返回给客户端之前,我们也可以执行一些操作。 请注意,这只是一个简单的示例,实际中间件的实现可能更加复杂,具体取决于你的需求和应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值