hcie实验-python部分(ensp版)

一、需求

  1. 定期对X园区内交换机X_T1_AGG1的关键运行状态进行监控(5min):
    电源状态
    风扇状态
    LACP 状态
    CPU/内存利用率
    OSPF邻居状态
  2. 对状态监控结果进行分析,并在风扇出现异常状态时(两个风扇状态全部为非normal)给出明确提示:“All fans are faulty .”
  3. 脚本所执行的监控相关命令不直接在代码中固定,而是以文件的形式保存,脚本从文件中读取命令并执行。
  4. 每隔 24h 保存一次X_T1_AGG1的配置文件,并通过安全的文件传输协议,将配置文件备份在电脑本地,以 [当天日期_设备名称] 的名称命名设备端以及本地的配置文件,如
    2021_10_20_X_T1_AGG1.zip、2021_10_20_X_T1_AGG1.bak
  5. 打开设备的NETCONF功能,并通过NETCONF配置X_T1_AGG1的日志主机为 10.1.60.2

二、配置步骤

1、模拟器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
aaa
undo local-user policy security-enhance
local-user python password irreversible-cipher Huwei@123
local-user python service-type ssh
local-user python level 3
#
stelnet server enable
sftp server enable
ssh user python authentication-type password
ssh user python service-type sftp stelnet
ssh user python sftp-directory cfcard:
ssh server-source -i GE1/0/0
#
#
user-interface vty 0 4
authentication-mode aaa
protocol inbound ssh

2、display文件1

1
2
3
4
5
6
7
8
9
10
screen-length 0 temporary
dis power
dis fan
dis lcap b
dis CPU
dis memory-usage
dis ospf peer b
sa
y
y

3、netconf文件

1
2
3
4
5
6
7
8
9
10
11
12
screen-length 0 temporary
sys i
aaa
local-user netconf password irreversible-cipher Huawei@123
local-user netconf service-type ssh
local-user netconf level 3
snetconf server enable
ssh user netconf authentication-type password
ssh user netconf service-type snetconf
netconf
protocol inbound ssh port 830
ret

4、python文件

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import paramiko
import time
import datetime
import os
import shutil
from ncclient import manager

ip = '172.16.20.20'
ssh_user = 'python'
ssh_pass = 'Huawei@123'
netconf_file = r'C:\Users\Administrator\Desktop\hcie\netconf.txt'
netconf_user = 'netconf'
netconf_pass = 'Huawei@123'
dis_file = r'C:\Users\Administrator\Desktop\hcie\display.txt'
bak_dir = r'C:\Users\Administrator\Desktop\hcie\backup'
vrp_file = r'vrpcfg.cfg'

syslog_file = '''
<config>
<syslog xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
<syslogServers>
<syslogServer operation="merge">
<ipType>ipv4</ipType>
<serverIp>10.1.60.2</serverIp>
<isDefaultVpn>false</isDefaultVpn>
<vrfName>_public_</vrfName>
<timestamp>UTC</timestamp>
<transportMode>tcp</transportMode>
</syslogServer>
</syslogServers>
</syslog>
</config>'''

class ssh():
def ssh_connect(ip,username,password):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip,username=username,password=password)
print(ip + ' login successful')
time.sleep(1)
return ssh

class AOM():
def dis_status(ip,username,password,file):
a = ssh.ssh_connect(ip,username,password)
cli = a.invoke_shell()
f = open(file,'r')
cmd = f.readline()
while cmd:
cli.send(cmd)
status = cli.recv(999999).decode()
print(status)
time.sleep(1)
fan = 'fan'
if fan in status:
n = status.count('Normal')
if n == 0:
print('All fan are faulty')
time.sleep(1)
cmd = f.readline()
print('dis_status OK')
f.close()
a.close()
def bak(ip,username,password,dir,file):
tran = paramiko.Transport(ip,22)
tran.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(tran)
local_file = dir + os.sep + file
remote_file = file
sftp.get(remote_file,local_file)
tran.close()
now_time = datetime.datetime.now().strftime('%Y_%m_%d')
shutil.move(local_file,dir + os.sep + now_time + '_X_T1_AGG1.bak')
print('bak OK')
time.sleep(1)

class NETCONF():
def netconf_config(ip,username,password,file):
a = ssh.ssh_connect(ip,username,password)
cli = a.invoke_shell()
f = open(file,'r')
cmd = f.readlines()
for i in cmd:
cli.send(i)
dis_config = cli.recv(999999).decode()
print(dis_config)
time.sleep(1)
if 'wait' in dis_config:
time.sleep(1)
print('netconf config OK')
f.close()
a.close()
def huawei_connect(ip,username,password):
return manager.connect(host=ip,port=830,username=username,password=password,
hostkey_verify=False,device_params={'name':'huawei'},
allow_agent=False,look_for_keys=False)
def set_syslog(ip,ssh_user,ssh_pass,netconf_file,netconf_user,netconf_pass,syslog_file):
NETCONF.netconf_config(ip,ssh_user,ssh_pass,netconf_file)
m = NETCONF.huawei_connect(ip,netconf_user,netconf_pass)
m.edit_config(target='running', config=syslog_file)
print('set_syslog OK')
time.sleep(1)
if __name__ == '__main__':
NETCONF.set_syslog(ip,ssh_user,ssh_pass,netconf_file,netconf_user,netconf_pass,syslog_file)
while True:
AOM.bak(ip,ssh_user,ssh_pass,bak_dir,vrp_file)
for i in range(288):
AOM.dis_status(ip,ssh_user,ssh_pass,dis_file)
time.sleep(300)