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的知识。