大家好,本文将围绕python 运维开发都需要掌握哪些展开说明,用python开发的运维管理系统是一个很多人都想弄明白的事情,想搞清楚python网络运维脚本需要先了解以下几个事情。
os
dir = os.path.isdir(dir):
(% dir)
:
(% dir)
系统内存与磁盘检测
psutil
memissue():
()
mem = psutil.virtual_memory()
memtotal = mem.total//memused = mem.used//membaifen = (mem.used/mem.total*) + (% memused)
(% memtotal)
(membaifen)
cuplist():
()
= psutil.disk_partitions()
diskuse = psutil.disk_usage()
diskused = diskuse.used / / / disktotal = diskuse.total / / / diskbaifen = diskused / disktotal * (% diskused)
(% disktotal)
(% diskbaifen)
memissue()
()
cuplist()
统计nginx日志前十ip访问量并以柱状图显示
matplotlib.pyplot plt
nginx_file = ip = {}
(nginx_file) f:
i f.readlines():
s = i.strip().split()[]
lengh = (ip.keys())
s ip.keys():
ip[s] = ip[s] + :
ip[s] = ip = (ip.items(), =e:e[], =)
newip = ip[::]
tu = (newip)
x = []
y = []
k tu:
x.append(k)
y.append(tu[k])
plt.title()
plt.xlabel()
plt.ylabel()
plt.xticks(=)
a,b (x,y):
plt.text(a, b, % b, =, = ,=)
plt.bar(x,y)
plt.legend()
plt.show()
test.png
查看网段里有多少ip地址
IPy
ip = IPy.IP()
(ip.len())
i ip:
(i)
gitlab钩子脚本,实现简单自动化操作
flask Flask,request,render_template,make_response,Response
json,os,re,requests
subprocess
app = Flask(__name__)
null = cmd = (,=[])
hello():
json_dict = json.loads(request.data)
name = json_dict[]
ref = json_dict[][:]
= json_dict[][]
name == ref == :
os.chdir(cmd)
s = subprocess.getoutput()
Response(s)
:
Response()
__name__ == :
app.run(=,=)
解析一组域名的ip地址
dns.resolver
collections defaultdict
hosts = [,]
s = defaultdict()
query(hosts):
host hosts:
ip = dns.resolver.query(host,)
i ip:
s[host].append(i)
s
i query(hosts):
(i,s[i])
清除指定redis缓存
redis
db = ()
r = redis.Redis(=,=,=)
id = ()
arg = + id + n = r.keys(arg)
i n:
(i.decode())
delid = ()
(% delid)
下载阿里云RDS二进制日志
base64,urllib.request
hashlib
hmac
uuid,time,json,wget
RDS_BINLOG_RELATE():
():
.access_id = .access_key = signed():
timestamp = time.strftime(, time.gmtime())
header = {
: ,
: ,
: ,
: timestamp,
: ,
: ,
: .access_id,
: ,
: ,
: (uuid.uuid1()),
: timestamp,
}
sortedD = (header.items(), =x: x[])
url = canstring = k, v sortedD:
canstring += + .percentEncode(k) + + .percentEncode(v)
stiinGToSign = + .percentEncode(canstring[:])
bs = .access_key + bs = (bs, =)
stiingToSign = (stiingToSign, =)
h = hmac.new(bs, stiingToSign, hashlib.sha1)
stiingToSign = base64.b64encode(h.digest()).strip()
header[] = stiingToSign
url = url + + urllib.parse.urlencode(header)
url
percentEncode(,store):
encodeStr = store
res = urllib.request.quote(encodeStr)
res = res.replace(, )
res = res.replace(, )
res = res.replace(, )
(res)
getBinLog():
binlog_url = .signed()
req = urllib.request.urlopen(binlog_url)
req = req.read().decode()
res = json.loads(req)
i res[][]:
wget.download(i[])
s = RDS_BINLOG_RELATE()
s.getBinLog()
阿里云通过465端口发送邮件
smtplib
sys
email.mime.text MIMEText
email.utils formataddr
my_sender = my_pass = my_user = subject = sys.argv[]
context = sys.argv[]
mail():
mail_msg = context
msg = MIMEText(mail_msg, , )
msg[] = formataddr([, my_sender])
msg[] = formataddr([, my_user])
msg[] = subject
server = smtplib.SMTP_SSL(, )
server.login(my_sender, my_pass)
server.sendmail(my_sender, [my_user, ], msg.as_string())
server.quit()
:
mail()
()
:
()
本文来自 七兮网络 转载请注明;
本文地址:http://qxhut.cn/?id=80。
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负python中使用turtle绘制半圆形。本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!