脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python自动巡检H3C交换机实现过程解析

Python自动巡检H3C交换机实现过程解析

2020-08-15 00:01ciscoxushaojie Python

这篇文章主要介绍了Python自动巡检H3C交换机实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.通过netmiko模块登录交换机,协议ssh,执行收集信息命令,保存至txt文件

2.过滤txt文件中的内容,保存到excel,使用xlwt模块实现。

3.sendmai发送excel邮件。或者发送给钉钉机器人也可以

4.使用crond定时发送巡检报表。

代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python3
#H3c交换机
import time
from netmiko import ConnectHandler
now = time.strftime("%Y%m%d",time.localtime(time.time()))
log_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
 
ip_list = [
  ['sw-001','192.168.1.1'],
  ['sw-002','192.168.1.2'],
]
 
SW = {
  'device_type':'hp_comware',
  'username':'admin',
  'ip':'',
  'password':"$password"
}
 
for ip_item in ip_list:
  SW['ip'] = ip_item[1]
  connect = ConnectHandler(**SW)
  print(log_time + 'Successfully connected to ' + ip_item[0])
  output = connect.send_command('system view')
  iproute = connect.send_command("display ip routing-table")
  version = connect.send_command('dis version')
  fan = connect.send_command('display fan')
  cpu = connect.send_command('display cpu-usage')
  mem = connect.send_command('display memory')
  env = connect.send_command('display environment')
  fo = open('xusj','w')
  fo.write(iproute)
  fo.write(fan)
  fo.write(cpu)
  fo.write(mem)
  fo.write(env)
  fo.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.51cto.com/xushaojie/2486217

延伸 · 阅读

精彩推荐