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.