python netmiko库H3C交换机自动化

 

import time
from netmiko import *
import sys
import os
import getpass

# 读取iplist.txt 文档里面的ip地址
iplist = open('iplist.txt', 'r', encoding='ascii')
ip_addr = iplist.readlines()
iplist.close()

cmdlist = open('cmdlist.txt', 'r', encoding='ascii')
cmd_line = cmdlist.readlines()
cmdlist.close()

# 遍历ip_addr这个列表里面的ip进行连接
for ip in iter(ip_addr):
    print(ip)
    try:
        S5130 = {
            'device_type': 'hp_comware',
            'ip': ip,
            'username': 'h3c',
            'password': 'h3c',
        }
        net_connect = ConnectHandler(**S5130)

        for cmd in iter(cmd_line):
            cmd_result = net_connect.send_command(cmd)
            print('-----------------------------------------------------------------')
            print(cmd)
            print(cmd_result)
            print('-----------------------------------------------------------------')
            print('')
        net_connect.disconnect()
    except (EOFError, NetMikoTimeoutException):
        print('Can not connect to Device')
    except (EOFError, NetMikoAuthenticationException):
        print('username/password wrong!')

 

 


iplist.txt

172.16.1.3
172.16.2.3

 


cmdlist.txt

display vlan
display version
display ip inter brief
disp cu

 

你可能感兴趣的:(python netmiko库H3C交换机自动化)