Planning the Application

Python 3: Automating Your Job Tasks Superhero Level: Automate Network Tasks with Python 3
5 minutes
Share the link to this page
You need to have access to the item to view this lesson.
One-time Fee
List Price:  $139.99
You save:  $40
List Price:  €128.79
You save:  €36.80
List Price:  £110.19
You save:  £31.48
List Price:  CA$190.57
You save:  CA$54.45
List Price:  A$209.11
You save:  A$59.75
List Price:  S$188.40
You save:  S$53.83
List Price:  HK$1,091.62
You save:  HK$311.91
CHF 90.88
List Price:  CHF 127.24
You save:  CHF 36.35
NOK kr1,067.14
List Price:  NOK kr1,494.05
You save:  NOK kr426.90
DKK kr686.38
List Price:  DKK kr960.96
You save:  DKK kr274.58
List Price:  NZ$228.66
You save:  NZ$65.33
List Price:  د.إ514.18
You save:  د.إ146.92
List Price:  ৳16,338.55
You save:  ৳4,668.49
List Price:  ₹11,658
You save:  ₹3,331.09
List Price:  RM655.85
You save:  RM187.40
List Price:  ₦210,474.96
You save:  ₦60,140
List Price:  ₨38,865.72
You save:  ₨11,105.28
List Price:  ฿5,043.69
You save:  ฿1,441.16
List Price:  ₺4,508.30
You save:  ₺1,288.17
List Price:  B$714.43
You save:  B$204.14
List Price:  R2,546.38
You save:  R727.58
List Price:  Лв251.52
You save:  Лв71.86
List Price:  ₩190,204.11
You save:  ₩54,347.91
List Price:  ₪517.99
You save:  ₪148
List Price:  ₱8,104.60
You save:  ₱2,315.76
List Price:  ¥21,798.54
You save:  ¥6,228.60
List Price:  MX$2,323.18
You save:  MX$663.81
List Price:  QR508.83
You save:  QR145.39
List Price:  P1,890.63
You save:  P540.21
List Price:  KSh18,338.69
You save:  KSh5,240
List Price:  E£6,552.93
You save:  E£1,872.39
List Price:  ብር8,014.55
You save:  ብር2,290.03
List Price:  Kz118,777.64
You save:  Kz33,938.89
List Price:  CLP$124,991.07
You save:  CLP$35,714.28
List Price:  CN¥993.35
You save:  CN¥283.83
List Price:  RD$8,173.51
You save:  RD$2,335.45
List Price:  DA18,828.31
You save:  DA5,379.90
List Price:  FJ$311.92
You save:  FJ$89.12
List Price:  Q1,084
You save:  Q309.73
List Price:  GY$29,190.54
You save:  GY$8,340.75
ISK kr13,827.61
List Price:  ISK kr19,359.21
You save:  ISK kr5,531.60
List Price:  DH1,389.16
You save:  DH396.93
List Price:  L2,472.24
You save:  L706.40
List Price:  ден7,927.38
You save:  ден2,265.13
List Price:  MOP$1,121.33
You save:  MOP$320.40
List Price:  N$2,552.76
You save:  N$729.41
List Price:  C$5,136.75
You save:  C$1,467.74
List Price:  रु18,602.11
You save:  रु5,315.27
List Price:  S/521.05
You save:  S/148.88
List Price:  K541.86
You save:  K154.83
List Price:  SAR525.01
You save:  SAR150.01
List Price:  ZK3,561.44
You save:  ZK1,017.62
List Price:  L640.73
You save:  L183.08
List Price:  Kč3,182.25
You save:  Kč909.28
List Price:  Ft49,718.89
You save:  Ft14,206.41
SEK kr1,068.43
List Price:  SEK kr1,495.85
You save:  SEK kr427.41
List Price:  ARS$124,135.76
You save:  ARS$35,469.89
List Price:  Bs964.05
You save:  Bs275.46
List Price:  COP$534,257.88
You save:  COP$152,656.01
List Price:  ₡71,411.70
You save:  ₡20,404.80
List Price:  L3,454.78
You save:  L987.15
List Price:  ₲1,045,465.66
You save:  ₲298,725.81
List Price:  $U5,431.06
You save:  $U1,551.84
List Price:  zł548.49
You save:  zł156.72
Already have an account? Log In


In this lecture, I will reveal the plan for this application. How is it going to work? What is the logic behind the program? What files and code do we need? Let's take this one step at a time. First of all, let's begin with the left side of this slide.

We will need three txt files for our application. The first file is going to contain the IP addresses of our network devices. Namely our three Arista virtual machines, each IP address will be positioned on a separate line. So from pythons point of view, we will have the newline character or backslash n as a delimiter for our IP addresses. The next file holds the username and password used to login to each Arista switch written on the same line separated by a comma. So this time, the comma will be regarded as a delimiter by Python.

Finally, the third text file will hold the commands That we want to execute on each of these switches. Maybe we want to see the results of a show command. Or even more exciting. Maybe we wish to configure a certain parameter on all the devices like for instance, the NTP server IP address. The final application will read through all of these files, extract the necessary data, then use that data to establish the SSH connection to each device and execute the commands. Sounds pretty cool right?

Now looking on the right side of this slide inside this blue rectangle, you can notice that we will have five Python scripts that are going to be imported to the final Python script called network app.pi. So basically, we are creating five modules, each with its own functionality and role and then use them inside the final program. This is a way more organized way of building an application than to just throw everything inside a single script. Also, troubleshooting any issues will become more effortless since localizing the problem will not require going through hundreds of lines of code to find the root cause. Next, let's have a brief look over what each of these modules does. First, we have the IP file script, since the final app will ask the user to manually input the name of the file holding the IP addresses.

The role of this script is to check whether the file specified by the user exists on the local file system and to also read the contents of the file if it does exist, and extract the IP addresses inside the text file in the form of a list. We will analyze the code inside each of these five Python modules in the next lecture, so don't worry about that. Now we're just taking a look at the big picture. The next script that we will use as a module is called ip addr valid dot p y. This script simply checks if each IP address in the first text file is a valid address. Meaning whether it has four octets as all IP addresses do, if each octet is in the correct format and of course, if the IP address is not a reserved or special IP address, we will talk more about how to check all this in one of the following lectures.

Next, the IP rich dot p y module will perform the job of checking whether each IP address in the list is reachable or not. By performing a ping and evaluating the result, this is crucial for our applications since we won't be able to connect and send commands to a device that is not reachable right. Then we have the SSH connection.pi module that performs several important tasks for our program. First, it checks if the other two text files meaning the one holding the username and password and the one containing the commands to send are valid and exists on the local file system. Otherwise, it will throw a customized error and quit the program. Next, the module contains a function that will read username and password from the file and use them to open an SSH connection to each device sends the desired commands and optionally return an output to the screen.

Finally, the Create threads dot p y module will create a thread for each device and perform the SSH connection command sending and output handling for all the devices simultaneously. At the end, we have the final application script network app.pi, where we will import the five modules we just discussed and call the necessary functions from within those modules in order to put all the code to work and interact with the Arista switches. So even if this may sound overwhelming at first, don't worry, we will take each file and analyze it line by line and then bring everything together towards the end of this section. Two more things I want to add before moving on. First, I already wrote the code inside this files and I will explain it to you in detail. In the coming lectures, I could have decided to write the code as I speak, but then it would have taken too long to finish this section and I know your time is precious.

However, I strongly advise you to write the code inside each lecture yourself in order to better understand it. Secondly, I have added the code for each relevant piece of this puzzle after every video that follows. And you can also just download all the modules and the final app down below, attached to the lecture called download the network application code. You can download these resources to have them at hand. But again, I would advise you to watch each video carefully and write the code as we go through each line. Practice makes perfect.

Having that said I'll see you in the next lecture.

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.