网络设备巡检自动化

 

 

import netmiko
from netmiko import ConnectHandler
import time
import os
import pandas as pd

today = time.strftime("%Y-%m-%d", time.localtime())
switch_with_authentication_issue = []
switch_not_reachable = []
show_cmd = 'terminal length 0\n' \
           'show run\n' \
           'show fan\n' \
           'show env all\n'
disp_cmd = 'screen-length 0 temporary\n' \
           'dis ver\n' \
           'dis env\n' \
           'dis cu\n'

def session(devlist):
    try:
        # list = line.split(",")
        hostname = list[0]
        devtype = list[1]
        ip = list[2]
        user = list[3]
        devpwd = list[4]
        # secret = list[5].replace('\n', '')
        secret = list[5]
        conn_s = ConnectHandler(device_type=devtype,
                                host=ip,
                                username=user,
                                password=devpwd,
                                secret=secret,
                                timeout=10,
                                conn_timeout=10
                                )
        conn_s.enable()
        time.sleep(1)
        conn_n = ConnectHandler(device_type=devtype,
                                host=ip,
                                username=user,
                                password=devpwd,
                                )
        # print(f'已成功登陆交换机{hostname}')
        if devtype == 'hp_comware':
            output = conn_n.send_command_timing('screen-length disable\n dis cu ')
        elif devtype == 'huawei_telnet':
            # output = conn_n.send_command_timing('screen-length 0 temporary\n dis cu')
            output = conn_n.send_command_timing(disp_cmd)
        elif devtype == 'juniper_junos':
            output = conn_s.send_command_timing('show configuration | display set | no-more')
        elif devtype == 'cisco_ios_telnet':
            # output = conn.send_command_timing('\n terminal length 0 \n show running-config \n show ver \n show env all')
            output = conn_s.send_command_timing(show_cmd)
        conn_s.disconnect()
        conn_n.disconnect()
        logname = "E:/Python/python_Shell/" + today + "/" + hostname + "_" + today + ".log"
        time.sleep(2)
        try:
            os.mkdir("E:/Python/python_Shell/" + today + "/")
        except OSError:
            pass
        wr = open(logname, 'a' or 'w')
        wr.write(output)
        wr.close()
    except netmiko.NetmikoAuthenticationException:
        # print(hostname + " 用户验证失败!")
        switch_with_authentication_issue.append(ip)
    except netmiko.ssh_exception.NetmikoTimeoutException:
        # print(ip + " ⽬标不可达!")
        switch_not_reachable.append(ip)
    except Exception as e:
        print(e, ip)

# devlist = open('E:/Python/python_Shell/device_pwd.txt', 'r')
devlist = pd.read_excel('E:/Python/python_Shell/devlist.xlsx')
for list in devlist.values:
    session(list)
for i in switch_with_authentication_issue:
    print("鉴权失败: "f"{i}")
for i in switch_not_reachable:
    print("不可达设备: "f"{i}")

 

posted @ 2022-08-29 18:01  邓聪聪  阅读(189)  评论(0编辑  收藏  举报