2019-06-14 Python Paramiko模块 在Huawei OLT巡检实例

Python publisher01 19℃

要对一批华为OLT(MA5800X7)进行巡检,其中有一项是检查lacp的配置。因为设备比较多,准备写一个python脚本来做。思路如下:
首先看下在OLT上要检查什么,用什么命令。

ol01.xxx.lon>enable
ol01.xxx.lon#display current-configuration | include link-aggregation
{ <cr>||<K> }:
  Command:
          display current-configuration
  It will take a long time if the content you search is too much or the string you input is too long,
you can press CTRL_C to break
 link-aggregation 0/8 0 egress-ingress workmode lacp-static
 link-aggregation add-member 0/8/0 0/8 1,2,3
 link-aggregation add-member 0/8/0 0/9 0,1,2,3
 link-aggregation lacp-key 0/8/0 1
ol01.xxx.lon#display link-aggregation ?
---------------------------------------------
  Command of privilege Mode:
---------------------------------------------
all                                    All of the link aggregation groups
frameid/slotid/portid<S><Length 5-18>  Frame ID/Slot ID/Port ID of link
                                       aggregation port
ol01.xxx.lon#display link-aggregation 0/8/0
{ <cr>||<K> }:
  Command:
          display link-aggregation 0/8/0
  link aggregation description:
  link aggregation mode: egress-ingress
  work mode:             lacp-static
  max link number:       -
  least link number:     -
  master port:           0/8/0
  -------------------------------------------------------------------------
  F/S           Port List
  -------------------------------------------------------------------------
  0/8           0,1,2,3
  0/9           0,1,2,3
  -------------------------------------------------------------------------

如果LACP正确配置的话,0/9/0-3 都会加入到0/8/0这个master port,就是说,配置里需要有” link-aggregation add-member 0/8/0 0/9 0,1,2,3“ 这一条。那么任务就可以简单描述为:
1、登录OLT
2、enable (dis cur | xx 需要权限)
3、执行命令:”display current-configuration | include link-aggregation”
4、获取输出,查找 ” link-aggregation add-member 0/8/0 0/9 0,1,2,3″ 关键字符串
5、打印查找结果,或者输出到文档
6、循环,继续下一台OLT
用Python实现的话,主要用到Paramiko模块,以及一些循环,正则表达式和标准输入输出。
下面的脚本是连接OLT,执行多条命令的范例:

#!/usr/bin/env python3
import paramiko
import time
import sys
import logging
from paramiko import AuthenticationException
from paramiko.ssh_exception import NoValidConnectionsError
#IP地址列表,可以通过读取EXCEL表格,或者读取yaml文件生成
allip = ["10.111.56.2","10.111.60.2"]
username = "anxing"
password = "password"
#命令列表,这里的命令供参考,配置设备同理
cmd = [
    "enable \n", "config \n",
    "link-aggregation add-member 0/8/0 0/9 0-3 \n \n ",
     "sysman service telnet disable \n y \n \n",
    "sysman firewall snmp enable \n",
    "sysman ip-access snmp xx.xx.xxx.1 xx.xx.xxx.254 \n",
    "quit \n save \n \n", "quit \n yes \n"
]
#定义函数
def mySshClient(ip, username, passwd, cmd):
    try:
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_client.connect(
            hostname=ip,
            username=username,
            password=password,
            allow_agent=False,
            look_for_keys=False)
        print("Sucessfully login to ", ip)  #banner
        command = ssh_client.invoke_shell()
        for i in range(len(cmd)):
            command.send(cmd[i])
            time.sleep(1)  #有时候输出较多,需要将等待延长几秒
            output = command.recv(65535)
            print(output.decode("utf-8"))  #需要的话可以使用标准输入输出写到文件里
        ssh_client.close()
    except AuthenticationException:
        logging.warning('username or password error')
    except NoValidConnectionsError:
        logging.warning('connect time out')
    except:
        logging.warning('unknow error')
        print("Unexpected error:", sys.exc_info()[0])
if __name__ == '__main__':
    for index in range(len(allip)):    #循环读取设备地址
        mySshClient(allip[index], username, password, cmd)

转载请注明:Python量化投资 » 2019-06-14 Python Paramiko模块 在Huawei OLT巡检实例

喜欢 (0)or分享 (0)