实验拓扑图
查询文本命令
display fower
display fan
display eth- trunk
display cpu
display memory
display ospf peer biref
(1)构造函数
def __init__ ( self, ip, username, password, netconf_user, netconf_password, type = 'huawei' ) :
self. ip= ip // 初始化参数
self. username= username
self. password= password
self. netconf_user= netconf_user
self. netconf_password= netconf_password
self. type = type
(2)ssh连接函数
def ssh_connect ( self) :
client= paramiko. SSHClient( )
client. set_missing_host_key_policy( paramiko. AutoAddPolicy)
client. connect( hostname= self. ip, port= 22 , username= self. username, password= self. password, look_for_keys= False )
print ( 'YES' )
return client
(3)发送配置命令
def sand_commands ( self, commands) :
ssh= self. ssh_connect( )
cli= ssh. invoke_shell( )
for i in commands:
cli. send( i+ '\n' )
time. sleep( 1 )
ssh. close( )
(4)下载配置文件
def get_file ( self, remote_file, local_file) :
ssh= self. ssh_connect( )
sftp= ssh. open_sftp( )
sftp. get( remotepath= remote_file, localpath= local_file)
ssh. close( )
(5) 定义备份配置文件方法
def backup_config ( self) :
today= date. today( )
remote_file= today. strftime( "%Y_%m_%d" ) + "_x_t1_agg1.zip"
local_file= today. strftime( "%Y_%m_%d" ) + "_x_t1_agg2.zip"
commands= [ "save" + '' + remote_file, 'y' ]
self. sand_commands( commands)
print ( 'save ok' )
self. get_file( remote_file, local_file)
print ( 'BACKUP OK' )
(6) 监控设备状态方法
def monitor ( self, file ) :
ssh = self. ssh_connect( )
cli = ssh. invoke_shell( )
cli. send( 'screen-length 0 temporary \n' )
time. sleep( 1 )
with open ( file ) as f:
lines = f. readlines( )
for line in lines:
cli. send( line)
time. sleep( 1 )
output = cli. rec( 999999 ) . decode( )
print ( output)
if "display fan" in line:
FAN_Normal = output. count( 'Normal' )
if FAN_Normal == 0 :
print ( "All fans are faulty" )
ssh. close( )
(7) 配置日志主机-1
def netconf_connect ( self) :
m = manager. connect(
host= self. ip,
port= '830' ,
username= self. netconf_user,
password= self. netconf_password,
hostkey_verify= False ,
device_params= { 'name' : self. type }
)
(8) 配置日志主机-2
Config_HostLog= """
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<syslog xmlns="http://www.huawei.com/netconf/vrp/huawei-syslog">
<syslogServers>
<syslogServer xc:operation="merge">
<ipType>ipv4</ipType>
<serverIp>10.1.0.6</serverIp>
<isDefaultVpn>false</isDefaultVpn>
<vrfName>_public_</vrfName>
<timestamp>UTC</timestamp>
<transportMode>tcp</transportMode>
</syslogServer>
</syslogServers>
</syslog>
</config>
"""
(9) 运行
import paramiko
import time
from ncclient import manager
from datetime import date
if __name__== '__main__' :
Device1= Device( '10.1.0.6' , 'py' , 'Huawei@123' , 'netconf' , 'Huawei@123' )
m= Device1. netconf_connect( )
reply= m. edit_config( target= 'running' , config= Config_HostLog)
print ( reply)
print ( 'Syslog host config ok' )
total_time= 0
while True :
try :
Device1. monitor( 'monitor.txt' )
time. sleep( 10 )
total_time+= 10
print ( "total_time={}" . format ( total_time) )
// 每次循环total_time加10 秒 满足能除86400 时间为一天 一备份
if total_time% 86400 == 0 :
Device1. backup_config( )
print ( 'Backup config ok' )
except KeyboardInterrupt:
print ( "program exited" )
break