GNS3 Talks: Python for Network Engineers with GNS3 (Part 3) - Remove Passwords and improve scripts

David Bombal
David Bombal
57.6 هزار بار بازدید - 7 سال پیش - 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 we program a Cisco IOSvL2 switch with VLANs by using Python on an Ubuntu Docker container.

Script used in this video is available on GitHub here:
https://github.com/davidbombal/python...


Transcription:

This is one of multiple videos teaching you network programmability using Python and GNS3. So currently we have two scripts: Python1script1 and Pythons1script1. What I'll do is copy the s1 script to a file called s1script2.py notice we've changed the extension to the py

If I try and run at that script notice we’re told that the command is not found.

We could still use Pythons1 and run the script this way and notice the changes have been made to the switch we can see here that the switch was configured. But we may want to improve things by simply running the script as follows.

So let's use nano to edit the script.

Notice the difference it's now showing the text in colors so that's a lot nicer but what I'm also going to do is a reference Python in the script.

Now in Unix and executable file that's meant to be interpreted can indicate which interpreter to use by having hash exclamation mark or pound-bang if you prefer at the start of the first line followed by the interpreter.

So as an example, how does the operating system know if this is a bash script or a Python script?

This allows us to indicate that information so I'm going to save the script. Now when I run it, it's still not running. A good Linux command is clear to clear the screen so at the
moment we have our script but it's not executable. I'm going to change the permissions on the script to make it executable.

So I'm going to specify that the script is now executable by adding the executable permission using change mod +x. Notice it's a lowercase x.

ls now shows us that the script color has changed so I can now run the script simply by entering its name. And notice the script run.

Now the best way to find files within this directory is to use ./ and then search for the name. Notice when I press tab it autocompletes but if I do this it's not going to autocomplete.
So get into the habit of using ./ for files in the local directory , I’ll break that script.

So to prove that this works, nano and specify the script. So let's edit the script and what I'll do is simply add another VLAN to our switch.

So I'm going to specify VLAN_7 and give it a name of Python VLAN_7. I'll save the script.

So on the switch show vlan brief shows us that we have VLANs 1 to 6 configured, that cat and the script name shows us that we're going to add another VLAN to the switch. So I'll run the script, enter username of david, enter my password of cisco.

In this example notice we are told that VLAN7 was created. on the switch we can see that David connected to vty 0 show vlan brief. We now have a new VLAN configured on the switch.

Now remember, I'm not trying to do everything perfectly from the beginning. Babies don't run marathons. Don't try and  run a long distance marathon when you start out, don't try to be the best programmer in the world. Just learn to get something done and then improve or to use a developer term iterate your program and refine it and make it better as you learn more.

So one of the things we may want to change here is to no longer embed our password of cisco. so we could delete to those two lines. Now before we delete stuff let's create a backup.

So copy S1script2. And let's call it S1script backup file. So we now have a backup of our file. If we have a problem we can always restore or copy back that backup file.

So nano again and let's edit our script, so what I want to do here is I want to delete these two lines. So ctrl K cuts those two lines. And I'm going to save this script now that's not going to work unless we do this change on the switch.

So on the switch we want to change the vty line to provide privileges as soon as we login.

So what we'll do is do that on the user account. So we've got a user called david and we'll say user david privilege 15 and to just prove that it works, show IP interface brief shows us the
IP address of the switch. So let's telnet to ourselves and ensure that it works manually first.

so david it's a bit of a typo, can't spell my own name, try again. Notice we are already in privilege mode so we can type conft straight away. So we don't need the option enable. Show line shows us that no one's connected at the moment. Let's try and run our script. So david, cisco. Looks good, we can see that David logged into the switch.
7 سال پیش در تاریخ 1396/01/18 منتشر شده است.
57,685 بـار بازدید شده
... بیشتر