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)
|