GNS3 Talks: Python for Network Engineers with GNS3 (Part 8) - Opening Files and spacing importance!
David Bombal
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: https://www.youtube.com/playlist?list=PLhfrWIlLOoKPn7T9FtvbOWX8GxgsFFNwn
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/d3190711ffe338b7a89e1d6bedc82b5d21789e73
Transcription:
This is one of multiple videos teaching you network programmability using Python and GNS3. So let's continue improving or iterating our Python script for network automation. At the moment our script looks as follows.
We are configuring multiple switches by using a loop. So we are saying for n in the range 72 to 77 configure switches with IP addresses 192.168.122.72 73 74 75 76 and 77.
In other words, these five Cisco switches will be configured in our topology. Now as many times in life there are other ways of doing things and perhaps better ways of doing things. One of the criticisms you may have of a solution like this, is what happens if your switches are not using IP addresses that are sequential. You may have switches and routers mixed in this range of IP addresses or you may only want to configure some Cisco switches and not others. So one of the things we can do is use a full loop and in addition we can read from a file. So I'm going to create a file called myswitches and in here I'm simply going to configure the IP addresses of our switches.
So as an example, on switch 1, the IP address is 192.168.122.72 So that's the first switch CTRL K CTRL U a few times. In our topology, it's fairly simple. Our IP addresses are in that range but it doesn't matter can configure any IP addresses.
So this is the IP address of the first switch and this is the IP address of the last switch. But this is a simple file and you could configure IP addresses in any range as needed in a file like this. So I'll save that file and then I'll just create a test. So let's call this testloop.py. So what we can do here is open a file and say F equals open and what we’re going to open is a file called myswitches. What I can do is create a full loop now. So full line in F, print line and then I can close the file. So what this is going to do is print every line in the file called myswitches. So I'll save the file. So what I can do now is run the Python script and notice it's showing the output of the file. Now puts a carriage return at the end you can remove that if you need to. But once again we’re opening a f ... https://www.youtube.com/watch?v=sa8Sti-90xg
61588689 Bytes