Connecting to the Network Device via SSH

Python 3: Automating Network Tasks Network Application #3 - Extracting Network Parameters & Building Graphs
6 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.85
List Price:  €130
You save:  €37.14
£79.68
List Price:  £111.56
You save:  £31.87
CA$136.70
List Price:  CA$191.38
You save:  CA$54.68
A$151.38
List Price:  A$211.94
You save:  A$60.56
S$134.98
List Price:  S$188.98
You save:  S$54
HK$781.15
List Price:  HK$1,093.64
You save:  HK$312.49
CHF 90.48
List Price:  CHF 126.67
You save:  CHF 36.19
NOK kr1,087.35
List Price:  NOK kr1,522.33
You save:  NOK kr434.98
DKK kr692.92
List Price:  DKK kr970.11
You save:  DKK kr277.19
NZ$166.35
List Price:  NZ$232.90
You save:  NZ$66.55
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,943.35
List Price:  ৳15,321.13
You save:  ৳4,377.77
₹8,337.18
List Price:  ₹11,672.39
You save:  ₹3,335.20
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,847.21
List Price:  ₨38,987.21
You save:  ₨11,140
฿3,675.50
List Price:  ฿5,145.85
You save:  ฿1,470.34
₺3,235.04
List Price:  ₺4,529.19
You save:  ₺1,294.14
B$507.19
List Price:  B$710.09
You save:  B$202.90
R1,850.71
List Price:  R2,591.07
You save:  R740.36
Лв181.51
List Price:  Лв254.13
You save:  Лв72.61
₩135,590.93
List Price:  ₩189,832.73
You save:  ₩54,241.80
₪371.66
List Price:  ₪520.34
You save:  ₪148.68
₱5,705.78
List Price:  ₱7,988.32
You save:  ₱2,282.54
¥15,304.96
List Price:  ¥21,427.56
You save:  ¥6,122.60
MX$1,696.83
List Price:  MX$2,375.63
You save:  MX$678.80
QR364.06
List Price:  QR509.70
You save:  QR145.64
P1,358.38
List Price:  P1,901.79
You save:  P543.40
KSh13,211.65
List Price:  KSh18,496.84
You save:  KSh5,285.18
E£4,788.95
List Price:  E£6,704.73
You save:  E£1,915.77
ብር5,723.72
List Price:  ብር8,013.44
You save:  ብር2,289.71
Kz83,591.64
List Price:  Kz117,031.64
You save:  Kz33,440
CLP$94,219
List Price:  CLP$131,910.38
You save:  CLP$37,691.37
CN¥723.65
List Price:  CN¥1,013.14
You save:  CN¥289.49
RD$5,794.19
List Price:  RD$8,112.10
You save:  RD$2,317.90
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.06
List Price:  Q1,085.12
You save:  Q310.05
GY$20,860.22
List Price:  GY$29,205.14
You save:  GY$8,344.92
ISK kr13,962.60
List Price:  ISK kr19,548.20
You save:  ISK kr5,585.60
DH1,005.73
List Price:  DH1,408.07
You save:  DH402.33
L1,766.28
List Price:  L2,472.87
You save:  L706.58
ден5,712.05
List Price:  ден7,997.10
You save:  ден2,285.05
MOP$802.57
List Price:  MOP$1,123.63
You save:  MOP$321.06
N$1,845.78
List Price:  N$2,584.16
You save:  N$738.38
C$3,670.67
List Price:  C$5,139.09
You save:  C$1,468.41
रु13,313.56
List Price:  रु18,639.52
You save:  रु5,325.95
S/372.22
List Price:  S/521.13
You save:  S/148.90
K385.44
List Price:  K539.63
You save:  K154.19
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,689.66
List Price:  ZK3,765.63
You save:  ZK1,075.97
L462.03
List Price:  L646.86
You save:  L184.83
Kč2,326.92
List Price:  Kč3,257.79
You save:  Kč930.86
Ft36,193.38
List Price:  Ft50,672.18
You save:  Ft14,478.80
SEK kr1,078.64
List Price:  SEK kr1,510.15
You save:  SEK kr431.50
ARS$87,815.44
List Price:  ARS$122,945.14
You save:  ARS$35,129.69
Bs690.51
List Price:  Bs966.74
You save:  Bs276.23
COP$388,367.89
List Price:  COP$543,730.59
You save:  COP$155,362.69
₡50,962.55
List Price:  ₡71,349.61
You save:  ₡20,387.06
L2,463.20
List Price:  L3,448.58
You save:  L985.38
₲746,475.93
List Price:  ₲1,045,096.16
You save:  ₲298,620.23
$U3,821.56
List Price:  $U5,350.33
You save:  $U1,528.77
zł401.98
List Price:  zł562.79
You save:  zł160.80
Already have an account? Log In

Transcript

Okay, first of all, in order to extract the network parameters that we need, we should connect to our network device and Arista switch, in our case via SSH. We've seen how to do this already in the first network application of this course, where we build several custom modules that work together to provide some verifications and read data from text files we defined before establishing the connection. So let's do a short recap. And then we will discuss what needs to be changed in the case of this application. So here are all the files you've already seen and used earlier in the course. Let's have a quick overview of each of them using notepad plus plus.

First of all, we have three text files that our application will read data from. So there's IP dot txt, which contains the IP address or addresses of the network devices that the application should connect to. In this case, we're going to work with just one of the originals. Which is namely 10 dot 10 dot 10 dot two. Secondly, we have user dot txt, which holds the username and password used to log into the device. The application will read the data inside this file and split the user name from the password using the split method and the comma as a delimiter.

The last text file that we need is CMD dot txt, which holds the commands to be sent to the device. Once the SSH connection is established, the commands will be inserted one for each line in the file. However, in our case, we will need a single command show processes stop once to obtain the CPU utilization value. So this is the only command inside the file. Now let's move on to our custom Python modules that we've built earlier in the course. And let's start with IP file valid.pi, which as you may recall, prompts the user to enter the path to the file holding the IP addresses, checks whether the file exists Or not on the local file system, and then reads all the IP addresses in the file, thus building and returning a list of IP addresses which is going to be used inside the main application.

Next, let's see the IP addr valid dot p y module which iterates over the list of IP addresses and verifies the validity of each address. The checks that this module performs are pretty straightforward. The IP address should have four octets should not be a special or reserved type like multicast broadcast, or loopback. And the octet should be integers between zero and 255. Otherwise in case one of these conditions isn't met, then the program will throw a customized error message and then quit its execution. The next custom module is IP rich dot p y.

The job of this module is to check the IP reachability to each of the IP addresses in the list by simply performing a ping sending an ICMP echo request to each day. Vice. If at least one of these devices fail to respond, then the application will generate a nice message to the user and then stop running until the connectivity issue has been solved. Next on our list of custom modules, we have SSH connection.pi. And let me scroll up to the top of this file. This module performs several very useful tasks.

First, it asks the user to enter the path to the file containing the username and password, and also do the one holding the commands to be sent. The script checks whether both of these files exist on the local file system, and in case one of them isn't found, then it lets the user know about the issue and quits the execution of the application using sis dot exit. The most essential part of this module is the SSH connection function, which reads the username and password from the text file opens the SSH connection to the network device and logs in using the credentials extracted from the user data. txt file enters the configuration mode of the device and send the commands it finds inside the CMD dot txt file. Next, this script retrieves and saves the output returned by the commands using the rec v method right here, and also checks if any CLS syntax error has occurred while executing the commands on the switch using this if else code block.

Next, this block of code right here, this one, we're going to discuss it in the next lecture. So let's keep it for now. At the end, the script makes sure that the SSH connection is properly closed using the close method. And using Param eco specific exception, the authentication exception right here, it throws an error if the login credentials were invalid. So we have this case covered as well. The last custom module for our application is the Create threads.py module, which as you may recall from our previous discussions, creates a thread for each connection to our network devices in order to Allow us to read from and write to multiple devices simultaneously.

I won't go over the code in this module since we've already discussed it twice throughout the course. Last but not least, we have our main application called network app.pi, which uses all the previously discussed modules and puts the logic and functionalities together into a single file. As you can see, apart from two built in modules that we need in the script, CS and time, we are also importing functions from each of the previously discussed custom modules. As you can see, right here using the from import statement, then using try except blocks to cover any keyboard interrupt exception, we are running this functions and finally creating the threads for each SSH connection. The only thing that differs here when compared with the same file in the first network application of the course, is this wild loop right here, but we're going to discuss that later in this section.

So don't worry about that. Okay, so this was a short recap of the files and code that we are going to use for connecting and interacting with our Arista switch. By the way, you can download all of the scripts and the necessary files for this application at the end of this section. 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.