使用Python和Netmiko自动执行Cisco路由器/交换机更改

本文介绍了如何使用Python和Netmiko库自动执行Cisco路由器和交换机的配置更改,以简化数据中心迁移中涉及的大量手动任务。通过编写脚本并创建包含目标IP地址和配置命令的文本文件,可以实现对多个Cisco IOS设备的自动化配置更新,从而节省时间和提高效率。
摘要由CSDN通过智能技术生成

We're migrating DataCenters. Anyone who has been involved with a migration knows it's not a fun task. We're also migrating subnets which makes the fun increase exponentially.

我们正在迁移数据中心。 任何参与迁移的人都知道这不是一件有趣的事情。 我们还正在迁移子网,这使得乐趣成倍增加。

One of the tasks I had today was to change the IP address where our Cisco routers are sending their error and system logs. In total, this is only a few commands per device and probably a 20-minute task. However, I'm teaching myself Python and wanted to both (a) learn something new and (b) create something that would make future changes easier.

我今天要做的任务之一是更改Cisco路由器发送错误和系统日志的IP地址。 总共,每个设备只有几个命令,大概是20分钟的任务。 但是,我正在自学Python,并且想(a)学习新知识,(b)创建可以使将来的更改变得更容易的东西。

Between Google and trial/error I've put together this process of automating change or gathering data from multiple Cisco IOS devices. If this can help you - please use it. God knows we all need a few extra minutes in our day.

在Google和审判/错误之间,我将自动更改或从多个Cisco IOS设备收集数据的过程组合在一起。 如果这可以帮助您-请使用它。 上帝知道我们每一天都需要多花几分钟。

建立 (Setup)

First, you'll need Python 3. I'm not going to go into detail on how to install that but here's an excellent guide.

首先,您将需要Python3。我不会详细介绍如何安装它,但这是一个很好的指南。

Next, you'll need 2 files. These will need to be in the same folder/directory as your script.

接下来,您将需要2个文件。 这些文件必须与脚本位于同一文件夹/目录中。

iplist.txt - which will be a list of the IP addresses that you want to connect to.

iplist.txt-这是您要连接的IP地址的列表。

configfile.txt - which will be the lines of config that you want to push to the routers.

configfile.txt-这是您要推送到路由器的配置行。

剧本 (The Script)

##imports python modules needed to work
from netmiko import ConnectHandler
import time, sys, getpass, paramiko 

 
##selects the correct Netmiko class based upon the device_type. I then define a network device dictionary consisting of a device_type, ip, username, and password.
device = {     'device_type': 'cisco_ios',     'ip': '192.168.43.10',     'username': 'username',     'password': 'password',     'secret':'password'
    } 

 
ipfile=open("iplist.txt")
print ("If you've not edited configfile.txt please stop and figure out what you're about to do...") device['username']=input("Enter your SSH username:  ") device['password']=getpass.getpass() device['secret']=input("Enter the enable password: ") configfile=open("configfile.txt") configset=configfile.read() ##reads the config file
configfile.close() 

for line in ipfile:  
 device['ip']=line.strip("\n")  print("\n\nConnecting Device.. ",line)  net_connect = ConnectHandler(**device)  net_connect.enable()  time.sleep(2)  print ("Passing configuration set ")  output = net_connect.send_config_set(configset)  print(output)  print ("Device Conigured ")  
 
ipfile.close() 

There isn't anything you need to edit inside the script. You can add/remove devices using the iplist.txt and you can change your commands using the configfile.txt.

您无需在脚本内进行任何编辑。 您可以使用iplist.txt添加/删除设备,也可以使用configfile.txt更改命令。

结果 (The Outcome)

Before we run our script.

在运行脚本之前。

test-rtr1#show run | i logging 10.199
test-rtr1# 

Then we run it.

然后我们运行它。

jacoby@jacoby-python:~/Documents$ python3 routers.py  If you've not edited configfile.txt please stop and figure out what you're about to do... Enter your SSH username:  admin Password:  Enter the enable password: thisisntmyenablepassword 

 
Connecting Device..  192.168.1.254
 
Passing configuration set  config term Enter configuration commands, one per line.  End with CNTL/Z. tuk-rtr1(config)#logging 10.199.0.87
tuk-rtr1(config)#end
tuk-rtr1#write mem
Building configuration... 

Device Conigured  jacoby@jacoby-python:~/Documents$   

And the outcome of running our script

运行脚本的结果

test-rtr1#show run | i logging 10.199
logging 10.199.0.87
test-rtr1# 

结论 (Conclusion)

This obviously is quite a bit of work to go through for a single router but as the number of devices increases, so does the time saved.

单个路由器显然需要完成大量工作,但是随着设备数量的增加,所节省的时间也会增加。

I hope this helps you save a few minutes of your day or at least learn a bit about Python.

我希望这可以帮助您节省几分钟的时间,或者至少了解一些关于Python的知识。

翻译自: https://www.experts-exchange.com/articles/33518/Automating-Cisco-Router-Switch-changes-with-Python-and-Netmiko.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值