python3 思科交换机配置保存及导出

本文介绍了如何利用Python3远程连接到思科交换机,进行配置操作,并详细阐述了如何保存和导出配置文件,为网络自动化管理提供便利。
摘要由CSDN通过智能技术生成
pip3 install netmiko
pip3 install openpyxl

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from netmiko import ConnectHandler
import os,time
import openpyxl


def Cisco(ip,username,password,secret):
    "交换机配置导出函数"
    cisco={
        "device_type":"cisco_ios",
        "ip":ip,
        "username":username,
        "password":password,
        "secret":secret
    }
    #print(cisco)
    #print(u'正在连接交换机:%s\n' % (ip))
    net_connect = ConnectHandler(**cisco)   #连接交换机
    net_connect.enable()                    #进入使能模式
    net_connect.send_command("wr")          #配置保存
    time.sleep(5)                           #等待配置保存
    commands = [
        #命令集合
        'show run',
        'show ip int brief',
        'show ip route',
        'show arp',
        'show mac add',
        'show version',
        'show logging'
    ]
    timestr = time.strftime('%Y-%m-%d', time.localtime(time.time()))    #记录当时年月日
    filename = u'%s_%s.txt' % (timestr,ip)                              #设置文件名格式
    path = os.path.dirname((os.path.dirname(os.path.abspath(__file__))))#记录保存路径
    file_path = os.path.join(path,file,filename)                        #记录文件保存路径(按需)
    #print(file_path)
    for cmd in commands:
        #循环执行命令集
        save = open(file_path,"a",encoding="utf-8")         #以追加方式添加
        result=net_connect.send_command(cmd)                #保存命令结果
        save.write("#%s\n" %cmd)                            #将命令写入文件
        save.write('%s\n' %result)                          #将命令结果写入文件
        save.write("\n")
    net_connect.disconnect()                                #断开连接

file = 'switch'+time.strftime('%Y-%m', time.localtime(time.time()))     #记录当前
path = os.path.dirname((os.path.dirname(os.path.abspath(__file__))))    #记录文件夹格式,按需更改
file_path = os.path.join(path, file)                        #保存文件夹格式
if not os.path.exists(file_path):                           #判断是否创建文件夹
    os.mkdir(file_path)
    #print("mkdir success")
else:
    pass
    #print("exist")


###读取文本###
f=open("soho-list","r",encoding="utf-8")
line=f.readline()
while line:
    #print(line)
    list = line.split()
    a = list[0]
    b = list[1]
    c = list[2]
    d = list[3]
    print(a,b,c,d)
    Cisco(a,b,c,d)
    line=f.readline()

###读取Excel###
# wb=openpyxl.load_workbook("password.xlsx")                  #读取excel表格
# sheet_list=wb.sheetnames                                    #获取excel表格所有sheet名称,返回列表形式
# for i in sheet_list:
#     sheet1=wb[i]
#     for row in range (1,sheet1.max_row+1):                  #循环读取单个sheet表格的每行数据
#         a = (sheet1['A%s' %row].value)
#         b = (sheet1['B%s' %row].value)
#         c = (sheet1['C%s' %row].value)
#         d = (sheet1['D%s' %row].value)
#         Cisco(a, b, c, d)
#     break                                                   #控制是否只用单个sheet

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值