架构图
架构图懒得画了,通过telnet备份交换机配置并通知备份结果,展示失败列表;根据命令不同,可以自定义修改用于任意品牌交换机的配置备份。
预览
系统组成
由两个文件组成 swtelnet.py
config.json
代码
由于代码在Windows平台编写,如果在linux平台运行,请把路径中的\
换成/
swtelnet.py
import os.path,telnetlib,time,requests,json,codecs
def get_config():
config = json.loads(open(path1+"\config.json", encoding='utf-8').read())
return config
def gettelnet_config():
nowtime = time.strftime("%Y%m%d", time.localtime())
try:
os.mkdir(path1 + "\\" +nowtime)
except:
None
nowdir = (path1 + "\\" +nowtime)
for host in config['ip']:
try:
tn = telnetlib.Telnet(host['host'], timeout=5)
tn.write(config['user'].encode('ascii') + b'\n')
time.sleep(3)
tn.write(config['passwd'].encode('ascii') + b'\n')
time.sleep(3)
tn.write(b'screen-length disable\n')
time.sleep(1)
tn.write(b'dis cur\n')
time.sleep(1)
tn.write(b'undo screen-length disable\n')
time.sleep(1)
tn.write(b'quit\n')
mac1 = tn.read_all()
f1 = open(nowdir+"\\"+host['host']+".conf", 'wb')
f1.write(mac1)
f1.close()
flog.write("finish " + host['hostname'] + " " + host['host']+"\n")
except:
flog.write("fail " + host['hostname'] + " " + host['host']+"\n")
def count_sw():
lines = open(path1 + "\\backup.log", "r", encoding='utf-8').readlines()
finish = 0
fail = 0
total = 0
failprint = ""
for line in lines:
line1 = line.split()
total = total + 1
if line1[0] == "fail":
failprint = failprint + str(line)
fail = fail + 1
else:
finish = finish + 1
return "交换机备份报告\n总计"+str(total)+" 完成"+str(finish)+" 失败"+str(fail)+"\n备份失败清单:\n"+failprint
class network:
def yiyan():
try:
url = 'https://v1.hitokoto.cn/?c=d&c=k'
response = requests.get(url)
res = json.loads(response.text)
text1 = res['hitokoto']
if res['from'] == None:
text2 = ""
else:
text2 = res['from']
if res['from_who'] == None:
text3 = ""
else:
text3 = res['from_who']
return text1 + " " + text2 + " " + text3 + "\n\n"
except:
return "一言API故障\n\n"
def tianqi():
try:
response2 = requests.get(config['weatherapi'])
data1 = json.loads(response2.text)
data2 = json.dumps(data1['now'])
data2 = json.loads(data2)
data3 = "环境温度" + data2['temp'] + " 体感温度" + data2['feelsLike'] + " 天气状况 " + data2[
'text'] + "\n风向 " + data2['windDir'] + " 风力等级" + data2['windScale'] + " 风速" + data2[
'windSpeed'] + " 湿度" + data2['humidity'] + " 能见度" + data2['vis'] + "公里\n\n"
return data3
except:
return "天气API故障\n\n"
def post_weixin(stats):
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=用自己的'
body = {
"msgtype": "news",
"news": {
"articles": [
{
"title": "AI智慧运维",
"description": network.tianqi()+network.yiyan()+stats,
"url": "90apt.com",
"picurl": "用自己的"
}
]
}
}
response = requests.post(url, json=body)
print(response.text)
print(response.status_code)
path1=os.path.abspath('.')
config = get_config()
flog = codecs.open(path1+"\\backup.log", 'w',encoding='utf-8')
gettelnet_config()
flog.close()
network.post_weixin(count_sw())
config.json
{
"user" : "用自己的账号",
"passwd" : "用自己的密码",
"weatherapi" : "https://devapi.qweather.com/用自己的API",
"ip" : [
{"host" : "172.16.1.6","hostname" : "仓库6"},
{"host" : "172.16.1.5","hostname" : "仓库5"}
]
}
总结
简单
评论