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
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.10
List Price:  €128.94
You save:  €36.84
£77.67
List Price:  £108.74
You save:  £31.07
CA$138.12
List Price:  CA$193.37
You save:  CA$55.25
A$152.38
List Price:  A$213.34
You save:  A$60.96
S$134.35
List Price:  S$188.10
You save:  S$53.74
HK$780.56
List Price:  HK$1,092.81
You save:  HK$312.25
CHF 88.35
List Price:  CHF 123.69
You save:  CHF 35.34
NOK kr1,098.66
List Price:  NOK kr1,538.17
You save:  NOK kr439.51
DKK kr687.32
List Price:  DKK kr962.28
You save:  DKK kr274.95
NZ$169.58
List Price:  NZ$237.42
You save:  NZ$67.84
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.91
৳11,746.62
List Price:  ৳16,445.74
You save:  ৳4,699.11
₹8,371.76
List Price:  ₹11,720.80
You save:  ₹3,349.03
RM465.70
List Price:  RM652
You save:  RM186.30
₦159,584.04
List Price:  ₦223,424.04
You save:  ₦63,840
₨27,830.93
List Price:  ₨38,964.42
You save:  ₨11,133.48
฿3,601.18
List Price:  ฿5,041.80
You save:  ฿1,440.62
₺3,299.80
List Price:  ₺4,619.86
You save:  ₺1,320.05
B$564.45
List Price:  B$790.25
You save:  B$225.80
R1,825.21
List Price:  R2,555.37
You save:  R730.15
Лв180.14
List Price:  Лв252.21
You save:  Лв72.06
₩138,584.75
List Price:  ₩194,024.19
You save:  ₩55,439.44
₪365.84
List Price:  ₪512.19
You save:  ₪146.35
₱5,843.06
List Price:  ₱8,180.52
You save:  ₱2,337.46
¥15,455.24
List Price:  ¥21,637.96
You save:  ¥6,182.71
MX$1,831.18
List Price:  MX$2,563.73
You save:  MX$732.54
QR364.64
List Price:  QR510.52
You save:  QR145.87
P1,361.94
List Price:  P1,906.78
You save:  P544.83
KSh13,073.69
List Price:  KSh18,303.69
You save:  KSh5,230
E£4,833.36
List Price:  E£6,766.90
You save:  E£1,933.54
ብር5,779.16
List Price:  ብር8,091.05
You save:  ብር2,311.89
Kz87,254.87
List Price:  Kz122,160.31
You save:  Kz34,905.44
CLP$94,704.52
List Price:  CLP$132,590.12
You save:  CLP$37,885.60
CN¥725.11
List Price:  CN¥1,015.19
You save:  CN¥290.07
RD$5,921.37
List Price:  RD$8,290.16
You save:  RD$2,368.78
DA13,436.79
List Price:  DA18,812.04
You save:  DA5,375.25
FJ$225.82
List Price:  FJ$316.16
You save:  FJ$90.33
Q775.39
List Price:  Q1,085.58
You save:  Q310.18
GY$20,916.55
List Price:  GY$29,284.01
You save:  GY$8,367.45
ISK kr13,788.62
List Price:  ISK kr19,304.62
You save:  ISK kr5,516
DH983.22
List Price:  DH1,376.54
You save:  DH393.32
L1,774.76
List Price:  L2,484.74
You save:  L709.97
ден5,676.16
List Price:  ден7,946.86
You save:  ден2,270.69
MOP$803.68
List Price:  MOP$1,125.18
You save:  MOP$321.50
N$1,847.94
List Price:  N$2,587.19
You save:  N$739.25
C$3,679.41
List Price:  C$5,151.32
You save:  C$1,471.91
रु13,388.03
List Price:  रु18,743.78
You save:  रु5,355.75
S/374.11
List Price:  S/523.77
You save:  S/149.65
K386.64
List Price:  K541.31
You save:  K154.67
SAR375.13
List Price:  SAR525.19
You save:  SAR150.06
ZK2,610.12
List Price:  ZK3,654.27
You save:  ZK1,044.15
L457.84
List Price:  L641
You save:  L183.15
Kč2,336.96
List Price:  Kč3,271.83
You save:  Kč934.87
Ft35,994.10
List Price:  Ft50,393.18
You save:  Ft14,399.08
SEK kr1,079.53
List Price:  SEK kr1,511.39
You save:  SEK kr431.85
ARS$92,940.28
List Price:  ARS$130,120.11
You save:  ARS$37,179.83
Bs690.73
List Price:  Bs967.06
You save:  Bs276.32
COP$404,134.11
List Price:  COP$565,803.93
You save:  COP$161,669.81
₡52,857.56
List Price:  ₡74,002.70
You save:  ₡21,145.14
L2,475.36
List Price:  L3,465.60
You save:  L990.24
₲758,175.48
List Price:  ₲1,061,476.01
You save:  ₲303,300.52
$U4,025.98
List Price:  $U5,636.54
You save:  $U1,610.55
zł393.60
List Price:  zł551.06
You save:  zł157.45
Already have an account? Log In

Transcript

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 valid.py 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

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.