王工已重新编写脚本,请查看最新文章
交换机自动备份配置(h3c)
python2备份基于CSDN@willwillwang
https://blog.csdn.net/wq298102526/article/details/108796824
python3自行编写
定时计划,每天7点备份,7点40发送告警
0 7 * * * python2 /root/swbackup.py > /root/swbackup.log
40 7 * * * python3 /root/swbackupweixin.py >> /root/swbackup.log
python2备份脚本 swbackup.py
利用telnetlib交互登录查看交换机配置并保存,可修改命令后用于任意品牌交换机
#!/usr/bin/python2
# -*- coding: UTF-8 -*-
import telnetlib
import time
import re
import codecs
import time
import os
now = time.strftime("%y%m%d")
path = "/root/backup/%s"%now
if not os.path.exists(path):
os.makedirs(path)
Hostall = """172.16.1.1
172.16.1.2
"""
Hostlist = Hostall.splitlines()
for Host in Hostlist:
try:
tn = telnetlib.Telnet(Host, timeout=15)
time.sleep(5)
tn.write(b'admin\n')
time.sleep(5)
tn.write(b'admin@123\n')
time.sleep(5)
tn.write(b'screen-length disable\n')
tn.write(b'dis cur\n')
tn.read_some()
tn.write(b'undo screen-length disable\n')
tn.write(b'quit\n')
mac1 = tn.read_all()
f1 = open('%s/%s'%(path,Host),'wb')
f1.write(mac1)
f1.close()
print ("%s finish"%Host)
except:
print("fail %s"%Host)
python3通知脚本
拥有python3企业微信应用通知和企业微信机器人通知,其中企业微信应用通知、温湿度使用了zabbix中现有脚本。
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import time,os,requests,json,subprocess
from datetime import datetime
from collections import Counter
lines = open("/root/swbackup.log", "r", encoding='utf-8').read().split()
finish = lines.count('finish')
fail = lines.count('fail')
total = str(finish + fail)
finish = str(finish)
fail = str(fail)
time_2 = time.strftime("%Y-%m-%d", time.localtime())
printfinish = (time_2+"-总计备份交换机"+total+"台-成功"+finish+"台-失败"+fail+"台")
os.system("/usr/lib/zabbix/alertscripts/weixin.py %s %s %s" % ("wangwangjie","交换机备份报告",printfinish))
response2 = requests.get("https://devapi.qweather.com/v7/weather/now?用自己的和风天气API")
data1=json.loads(response2.text)
data2=json.dumps(data1['now'])
data2=json.loads(data2)
data3 ="早上好! \n当前天气情况\n环境温度"+data2['temp']+" 体感温度"+data2['feelsLike']+" 天气状况 "+data2['text']+"\n风向 "+data2['windDir']+" 风力等级"+data2['windScale']+" 风速"+data2['windSpeed']+" 湿度"+data2['humidity']+" 能见度"+data2['vis']+"公里\n"
data4 = "备份交换机"+total+"台-成功"+finish+"台-失败"+fail+"台\n"
data5 = "机房温度"+str(os.popen("/etc/zabbix/script/get_temp.sh").read())+"机房湿度"+str(os.popen("/etc/zabbix/script/get_hum.sh").read())
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=用自己的企业微信机器人通知'
body = body = {
"msgtype": "news",
"news": {
"articles" : [
{
"title" : time_2,
"description" : data3+data4+data5,
"url" : "90apt.com",
"picurl" : "微信机器人上方图片"
}
]
}
}
headers = {"Content-Type": "application/json"}
response = requests.post(url,json=body,headers=headers)
print(response.text)
print(response.status_code)
评论