Okay, so the file holding the IP addresses is valid, the addresses aren't in the correct format and are not reserved ones. Now before opening the SSH connection to each device and sending commands, we should make sure that all the Arista switches are reachable. For this we will create a new module called ip rich.pi. Inside this module, we are going to use the sub process Python built in module right here to perform the ping operation to each device. So again, we are creating a function called IP reach that takes a single parameter the list of IP addresses generated by the read lines method after reading the text file. Next, we are again iterating over this list, and stripping away the newline characters at the end of each IP address in the list using the our strip method.
Now it's time to ping each device. For this we are using the call method. As you can see right here, from within the sub process module, and we're passing several arguments to this method, let's take them one at a time. First in between quotes. Because we are dealing with a string, we enter the command to be executed. So we have being then the string format operator present s, and then we add in backslash, N, and two, which means the number of echo requests to send to each device.
In our case, we want two packets to be sent out to the switch. Next, we enter the present format operator, and in between the parentheses we are specifying a single argument, the IP address to ping. The next two arguments that you can see right here, with STD out and STD air being both assigned to dev null are suppressing any output or errors of the ping command, because we don't want any messages generated by the ping command to interfere with the output of our application. So let me show you What I'm talking about in the windows cmd. So let's say we have being 10 dot 10 dot 10 dot 10. We don't want any of this output when running our application, because we want to keep the output clean.
So that's why we are suppressing any messages or errors inside our code. Okay, now back to the module. After pinging each device using the call methods, we are starting the response type using the ping reply variable right here with the purpose of using it further into the code. Now regarding the possible reply types of a ping operation, I have attached a link to this lecture in order for you to see all of the available types. However, you should keep in mind that although there are many types of replies that an echo request can receive, we're actually interested in only one type type zero, which is echo reply. This means that the ping was successful, and the device is reachable otherwise for any other type of reply, It means that there is a connectivity issue and we should troubleshoot our network setup.
So now we should translate this to Python code using Of course, an if else code block. Therefore, if the value stored using the ping reply variable is zero, so if being reply equals equals zero, then the switch is reachable, and we will print out a message. Then, using the continue statement right here, we move to evaluate the reachability of the next IP address in the list. Otherwise, if a reply type different than zero is received, then we print out an explicit message to the screen and quit the program allowing the user to troubleshoot his or her connection or network. performing this verification process is crucial for the success of our application. Otherwise, in case of any configuration failure, we may not know what has caused the issue and the troubleshooting time may increase.
I'll see you in the next lecture.