#!/bin/python # -*- coding:UTF-8 -*- # pip install netmiko from netmiko import ConnectHandler, SSHDetect from netmiko.exceptions import NetMikoAuthenticationException, NetMikoTimeoutException import threading import datetime import queue import re, os import logging logging.basicConfig(filename='test.log', level=logging.DEBUG) logger = logging.getLogger("netmiko") def conn_info(ip): dev = { # 'device_type': 'huawei_serial', # 指定设备类型,串口方式登陆,默认连接COM1,波特率9600 # 'device_type': 'huawei_telnet', # 指定设备类型,该类型适用于huawei设备,telnet方式 'device_type': 'huawei', # 该类型适用于huawei设备,ssh方式 'host': ip, 'port': 1022, # ssh默认使用22,telnet默认使用23 'username': 'admin', # 设备管理员用户 'password': 'TJuOO8Uz', # 管理员用户密码 } return dev def conn_dev(dev_q): while not dev_q.empty(): dev_ip = dev_q.get() try: dev_dict = conn_info(dev_ip) # guesser = SSHDetect(**dev_dict) # best_match = guesser.autodetect() # print('best_match is:{}'.format(best_match)) # print('all guessers is:{}'.format(guesser.potential_matches)) # dev_dict['device_type'] = best_match dev_conn = ConnectHandler(**dev_dict) print("[%s] Connected." % dev_ip) output = dev_conn.send_config_from_file('cmds.txt') print(output) output = dev_conn.save_config() print(output) dev_conn.disconnect() print('[%s] done.\n' % dev_ip) except NetMikoAuthenticationException: print("[%s] Error! Please check username or password ..." % dev_ip) except NetMikoTimeoutException: print("[%s] Error! Connect time out ..." % dev_ip) except Exception as e: print('[%s] Error:%s' % (dev_ip, e)) if __name__ == "__main__": devs_ip = ['192.168.12.34', '192.168.12.35', '192.168.13.19'] # 需进行配置的交换机ip地址列表 devs_q = queue.Queue() for dev in devs_ip: devs_q.put(dev) max_conn = 15 # 同时操作交换机数量(可增加或减少) ts = [] # 线程集合 for i in range(max_conn): t = threading.Thread(target=conn_dev, args=(devs_q,)) t.start() ts.append(t) for t in ts: t.join() print("Done.")
标签:netmiko,ip,dev,模块,print,import,conn From: https://www.cnblogs.com/xwupiaomiao/p/17727371.html