python 登录华为AC-过滤掉线AP-通过飞书发出
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
from netmiko import ConnectHandler
import requests
import json
def ssh_login_device(HOSTINFO,config_commands):
try:
connect=ConnectHandler(**HOSTINFO)
except:
return "登录AC失败"
try:
for i in config_commands:
output=connect.send_config_set(i,cmd_verify=False)
connect.save_config()
return output
except:
return "执行命令,获取结果失败"
def re_all_out_str(yuan):
try:
out_rest1 = re.findall(r"0{1}\s{1}\w{2,5}.{1,}[\u4e00-\u9fa5]", yuan)
out_rest2 = re.findall(r"0{1}\s{1}\w{2,5}.{1,}default", yuan)
all_out = out_rest1 + out_rest2
last_out = [i[2:] for i in all_out]
last_out_str = '\n'.join(last_out)
return last_out_str
except:
return "正则匹配失败"
#个人测试飞书机器人
def send_message_to_yyy_alarm(message):
url = " "
payload_message = {
"msg_type": "text",
"content": {
"text": message
},
}
headers = {
'Content-Type': 'application/json'
}
try:
requests.request("POST", url, headers=headers, data=json.dumps(payload_message))
except:
print("发送飞书消息失败")
if __name__ == '__main__':
SW = {
'device_type': 'huawei',
'ip': '',
'username': '',
'password': '',
}
config_commands = ['display ap all | exclude nor', ]
rest = ssh_login_device(SW,config_commands)
message = re_all_out_str(rest)
send_message_to_yyy_alarm("掉线AP为:\n" + message)
结合zabbix自动运行命令
![在这里插入片描述](https://img-blog.csdnimg.cn/0508b24d31b240c7be0ddf954256a739.png