HCIE-python

实验拓扑图

在这里插入图片描述

查询文本命令

#文本名称monitor.txt
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连接函数

    #ssh连接方法
    def ssh_connect(self):
        #初始化实例 连接方式
        client=paramiko.SSHClient()
        #链接到一个未知的主机时设置策略为,自动添加公钥策略。不添加拒绝连接
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        #通过用户名和密码的方式登录主机,关闭密钥 look_for_keys:默认是False。如果python环境运行在Linux下会报错
        client.connect(hostname=self.ip,port=22,username=self.username,password=self.password,look_for_keys=False)
        #登录成功回显
        print('YES')
        return client

(3)发送配置命令

    #发送配置
    #commands为命令集
    def sand_commands(self,commands):
        #连接设备
        ssh=self.ssh_connect()
        #SSH shell通道
        cli=ssh.invoke_shell()
        #循环输入到连接设备
        for i in commands:
           cli.send(i+'\n')
           #等待1秒
           time.sleep(1)
        ssh.close()

(4)下载配置文件

    #定义下载配置文件
    def get_file(self,remote_file,local_file):
        ssh=self.ssh_connect()
        #进打开sftp通道
        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)
               #执行查询风扇一个选项时 回显结果里FAN_Normal变量为0证明所有风扇故障
               if "display fan" in line:
                   FAN_Normal = output.count('Normal')
                   if FAN_Normal == 0:
                         print("All fans are faulty")
        ssh.close()

(7) 配置日志主机-1

    # 定义netconf连接方法 参数设置
    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

 #配置日志主机 ensp ce12800 真机需替换  文档API 查询文档
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
#netconf
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
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值