Social Icons

Thursday, November 19, 2015

Check Out My Brand New Website! http://trendelearning.com/

Check out my brand new website! http://trendelearning.com/
Also comes with a special promotion for all my courses!

Python Tutorial: Python Network Programming - Build 7 Apps - Only $15!
Python Tutorial: Python 100% Hands-On - Learn by Coding - Only $15!
Udemy Marketing Tips For Beginners: Start Making Money Now! - Only $15!
Enroll now, grow your skills and improve your life or career! Let's get started!

Visit my official website to see all my courses: http://trendelearning.com/

Follow me on LinkedIn: https://ro.linkedin.com/in/tmihaicatalin
Follow me on Twitter: https://twitter.com/TeoMc15
Follow me on Facebook: https://www.facebook.com/trendelearning
Follow me on Google+: https://plus.google.com/u/0/+MihaiCatalinTeodosiu
Follow my Google Business Page: https://plus.google.com/114777462555390300433
Follow me on Xing: https://www.xing.com/profile/MihaiCatalin_Teodosiu
Follow my blog: https://labs4ccnp.blogspot.com


Thursday, November 12, 2015

Python Tutorial: Python Network Programming - Build 7 Apps - Application #7 - Config File Comparator

This is a lecture from my “Python Tutorial: Python Network Programming - Build 7 Apps” course, which you can find at a huge discount (only $15, limited time offer) on my official website:

http://trendelearning.com/

Sign up for the FULL course and get:

▪ Python Tutorial - App#1: Subnet calculator.
▪ Python Tutorial - App#2: Configuring multiple network devices concurrently via SSH or Telnet.
▪ Python Tutorial - App#3: DHCP client simulator for testing a DHCP server in the local network.
▪ Python Tutorial - App#4: Collecting information from routers and storing it in a MySQL database.
▪ Python Tutorial - App#5: OSPF network discovery via SNMP. Building the OSPF topology.
▪ Python Tutorial - App#6: Basic network sniffer.
▪ Python Tutorial - App#7: Configuration file comparator.
▪ High quality video, audio and text lectures and relevant and updated content!
▪ Unlimited, LIFETIME ACCESS to the course!
▪ 30 day money back guarantee, FULL REFUND, no questions asked!
▪ Instant and FREE ACCESS to any course updates!
▪ My FULL SUPPORT regarding any issues or suggestions related to the course!

Python (2.7.3) programming course aimed not only at network professionals, but at anyone having little or no experience in coding or automation and a great desire to start learning Python from scratch. This hands-on training takes you from "Hello World!" to complex network applications in less than 15 hours.

This Python programming and network automation course is especially suitable for Network Professionals (CCNA, CCNP or CCIE level).

First, you will learn and practice every Python key concept, which is explained in one or more video lectures, followed by a short quiz. 

Each video is filled with relevant examples, in a learn-by-doing fashion and the quizzes will help you consolidate the main ideas behind each Python topic.

After laying the foundation (and also exploring some advanced Python topics), you will dive right into the real-life network scenarios and apply your knowledge to build 7 great network tools.

Equipped with working files, network topologies and Python code samples (in .pdf and .py formats), you will be able to work alongside me on each lecture and each application. I will provide a virtual machine with all the Python modules already installed and also the full code for each application, so you can save time and start coding and testing on the spot.

We will use emulated routers in GNS3 to test our Python applications in a network environment, so you can see the actual results of running your code.

Join thousands of successful students who have decided to learn Python, upgrade their networking skills and boost their careers using this 100% hands-on course!

Enroll now, grow your skills and improve your life or career! Let's get started!

Visit my official website to see all my courses: http://trendelearning.com/

Follow me on LinkedIn: https://ro.linkedin.com/in/tmihaicatalin
Follow me on Twitter: https://twitter.com/TeoMc15
Follow me on Facebook: https://www.facebook.com/trendelearning
Follow me on Google+: https://plus.google.com/u/0/+MihaiCatalinTeodosiu
Follow my Google Business Page: https://plus.google.com/114777462555390300433
Follow me on Xing: https://www.xing.com/profile/MihaiCatalin_Teodosiu
Follow my blog: https://labs4ccnp.blogspot.com


Application #7 - Config File Comparator

This application connects to a router in the network via Telnet, extracts the output of “show running-config” and “show startup-config”, filters the irrelevant lines and finally compares the configurations. Now, I know this can be accomplished using the “show archive config differences” command in Cisco CLI, but I wanted you to know how can this task be accomplished using Python.

As with the other applications in this course, the full code is available for download.

Based on what you have learned so far in the course, it’s your job now to study, understand and test the code against a network device, as you’ve seen me doing with the previous applications.

Feel free to alter the code in any way you want. New functionality of any kind is welcome, enhancements as well. Just make sure to adapt your code to the command output format.

Also, please read the first 13 lines in the code carefully, as they are a good introduction to the code that follows. As you can see, the first thing you should do is configure Telnet access on the router and the username and password:

username teopy privilege 15 password 0 python
line vty 0 4
   privilege level 15
   login local
   transport input telnet ssh

At line 27, I have defined the ip_validity() function, which takes care of checking whether the IP address of the router, which the user enters at the prompt, is valid or not. You have already seen this kind of validity check in action in the previous applications, so there is nothing new here. The same comment is valid for the file_validity() function (line 46). Both functions are defined at this point and will be called later in the code.

At line 61, the telnet() function is defined, which takes a single parameter: command. The value of this parameter will be passed to the connection.write() method at line 96.

Starting with line 108, I defined the user menu, which will accept 3 options, except e - Exit program:
1 - Compare running-config with startup-config
2 - Compare running-config with local file
3 - Compare startup-config with local file

I had treated only the first option, comparing the running-config with the startup-config - lines 115-196, leaving you with the job of coding and testing the other two options, having my code from option 1 as a guideline.

Now, let’s look at option 1 for a bit. First, I called the ip_validity() function to get this out of the way. Next, a very important step, I called the telnet() function for each of the two commands I am interested in, and saved the returned output to a separate variable: output_run for the running-config and output_start for the startup-config.

Then, I have created (opened for writing) two files, each of them storing the output of the corresponding command. The file names are intuitively chosen. Don’t forget to close the files after writing the contents of those variables, to save the information.

Next, I opened the files for reading and used the readlines() method on each file object to store the lines in each file as elements of a list. Of course, then I closed the files.

Then, using a for loop, I have filtered the lines in each file which were of no interest to our goal. We are only interested in the lines starting with the one defining the IOS version: “version 12.4” for example. That is actually the first relevant line in each file.

Now, after “cleaning” the files, we are left with only the pure router configurations. It’s time to create a new file (file_diff.txt), in which all the config differences are going to be stored. Actually, we are going to compare the two lists obtained with the readlines() method.

Finally, using list comprehensions, we are going to find the lines in the running-config which are not present in the startup-config and vice versa. In case there are multiple differences, we use a for loop to iterate over the lists and then print those differences directly into the file_diff.txt file., one per line As stated in the code, the rule is:

A "+" sign means the line is present in the RUNNING-CONFIG but not in the STARTUP-CONFIG
A "-" sign means the line is present in the STARTUP-CONFIG but not in the RUNNING-CONFIG

Now, let’s make a quick test. If you have just started the router and made no config yet, then the startup-config and running-config are the same. No surprise here. But, to make the test more relevant, let’s configure a few things before starting the comparison, without saving the changes to the startup-config. So, let’s go to router R1:

R1(config)#username udemy1 password udemy
R1(config)#username udemy2 password udemy
R1(config)#username udemy3 password udemy

Now, these three configurations are the differences between the startup-config and the running-config. We should see them after running our program, saved in the file_diff.txt file. Let’s test this:

root@debian:/home/debian/workingdir# python ConfigFileComp.py

Use this tool to:
1 - Compare running-config with startup-config
2 - Compare running-config with local file
3 - Compare startup-config with local file
e - Exit program

Enter your choice: 1
Enter an IP address: 192.168.2.101

Please wait while the config file is being analyzed...

Use this tool to:
1 - Compare running-config with startup-config
2 - Compare running-config with local file
3 - Compare startup-config with local file
e - Exit program

Enter your choice: e
Exiting... See ya...

Now let’s check the results. We should see all three commands with a “+” sign, right?

root@debian:/home/debian/workingdir# cat file_diff.txt
+username udemy1 password 0 udemy

+username udemy2 password 0 udemy

+username udemy3 password 0 udemy

root@debian:/home/debian/workingdir#

...and success! As expected, the three commands are marked as differences, in the file.

Please read all the comments in the code very carefully and test/debug the code as you write options 2 and 3 in the menu, before posting any questions. Have fun, everyone!!!

Thank you!