你必須知道的python運維常用腳本!(日常更新)

github地址:https://github.com/opsonly, 上面是一個基於python3.7django2.1的多人博客系統,喜歡的可以給個star~



判斷是否是一個目錄

#!/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()

test.png



查看網段裏有多少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-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()

12-27更新

阿里雲通過465端口發送郵件

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018-12-26 15:41
# @Author  : opsonly
# @Site    : 
# @File    : aliSendMail.py
# @Software: PyCharm

import smtplib
import sys
from email.mime.text import MIMEText
from email.utils import formataddr

#發件人郵箱賬號
my_sender = 'xxxxxxx'

#發件人第三方客戶端授權碼
my_pass = 'xxxxxx'

#收件人郵箱賬號
my_user = 'xxxxxxxx'

#接收參數並定義標題和內容
subject = sys.argv[1]
context = sys.argv[2]

def mail():

    mail_msg = context

    #發送信息
    msg = MIMEText(mail_msg, 'html', 'utf-8')
    msg['From'] = formataddr(["dashui", my_sender])
    msg['To'] = formataddr(["dashui", my_user])
    msg['Subject'] = subject

    #因爲阿里雲25號端口默認關閉,採用465端口
    server = smtplib.SMTP_SSL("smtp.163.com", 465)
    server.login(my_sender, my_pass)
    server.sendmail(my_sender, [my_user, ], msg.as_string())
    server.quit()

try:
    mail()
    print('郵件發送成功')
except:
    print('郵件發送失敗')

喜歡我寫的東西的朋友可以關注一下我的公衆號,上面有我的學習資源以及一些其他福利:Devops部落
你必須知道的python運維常用腳本!(日常更新)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章