GNS3 Talks: Python for Network Engineers with GNS3 (Part 9) - Backup switch configurations.

David Bombal
David Bombal
0 بار بازدید - 8 سال پیش - Udemy: Get the course for
Udemy: Get the course for $10 here: https://goo.gl/QYC988 GNS3 Academy: Get the course for $10 here: https://goo.gl/vnZJhg More free Python videos here:    • Python Programming for Network Engineers   Learn Python programming with GNS3. In this series of videos I will show you how you can quickly and easily program Cisco networks using Python. In this video I show you how to open a file and leverage that for configuring Cisco switches. I also demonstrate the importance of spacing in Python. Code is on GitHub here: https://github.com/davidbombal/pythonvideos/commit/13574b46f5be90c3316a5a93c1addc5a63e043c8 Transcription: I've had this question asked a number of times: “David can you show us how to backup the conflicts of multiple switches using Python?” So in this video I'm going to show you a quick demonstration of how you can backup the configuration of multiple devices using Python. This is One of multiple videos teaching you network programmability using Python and GNS3. So in this topology we have five switches, Switch 1, Switch 2, Switch 3, 4 and 5, which are Cisco IOSv Layer 2 switches. Previously, we created various scripts. One of the files is switchloopfile.py So let's have a look at that file This script is going to open a file called myswitches and for each align in the file, it's going to telnet to a switch and then it's going to configure various VLANs. So let's have a look at myswitches, that's a list of IP addresses of switches in the network. What I'll do now is copy switchloopfile.py to getconfigs.py. So nano getconfig.py There's some redundant codes. What I'll do is remove imports. Let's also add some comments to this code to improve it. So for this first piece of code we'll ask for username and password. This will open a file called myswitches. Now per Python best practices, you can use either single quotes or double quotes but it's a good idea to be consistent so let me fix that and make it consistent. So what happens here is we are asking the user to enter the username and password then we open a file called myswitches and for each align in the file which consists of an IP address, it's going to print out some information. So let's say get running config from switch and then it will print out the IP address of the switch. Then what it's going to do is set host equal to the line in the file. Now I'm going to strip redundant information in the line of the file such as hidden spaces in the file. So when you retrieve an IP address from a file, it's a good idea to strip any whitespace so that you only get the IP address of the device. Then we're going to tell it to the device we're going to enter a username which was previously requested over here so that variable stores the username and we are going to write the username to the switch right to the password to the switch and now rather than doing conft, what we’re going to do here is type terminal length 0. The reason for doing that, is when you type show run on the switch by default, it only shows you a certain number of lines and you have to press spacebar multiple times to see the full configuration. But if you type terminal length 0 and then type show run it's going to show you the full running config in one go and that's what we want to do. We don't want to program the script to have to look for more and then press spacebar. We might as well just to show the entire output of the running config in one go. So I'll copy that to another line. Once we've typed terminal length, we can type shall run. We don't need any of this extra configuration. So essentially, what we're doing is typing terminal length 0 show run and we'll exit out of the telnet session. Now we want to store that information somewhere, so what I'm going to do is create another variable called read output equal TN, TN is the variable we're using for our telnet session. So notice over here TN equals telnet a library telnet host. Host being the IP address found in each line of our file. So we're going to read all the output and then we're going to save that output to a file. So I'm going to open up a file called switch + the hostname which is the IP address of the switch. I'm going to write to the file. Now previously when I said F equals open myswitches the default option here is read-only. So you could specify R here for read-only but I'm not going to bother with that the default is read-only.
8 سال پیش در تاریخ 1396/01/24 منتشر شده است.
0 بـار بازدید شده
... بیشتر