Checking IP Address Reachability

Python 3: Automating Your Job Tasks Superhero Level: Automate Network Tasks with Python 3
3 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.82
You save:  €36.80
List Price:  £109.74
You save:  £31.35
List Price:  CA$191.13
You save:  CA$54.61
List Price:  A$210.68
You save:  A$60.19
List Price:  S$188.83
You save:  S$53.95
List Price:  HK$1,092.95
You save:  HK$312.29
CHF 91.34
List Price:  CHF 127.88
You save:  CHF 36.54
NOK kr1,052.31
List Price:  NOK kr1,473.28
You save:  NOK kr420.96
DKK kr686.58
List Price:  DKK kr961.24
You save:  DKK kr274.66
List Price:  NZ$227.97
You save:  NZ$65.13
List Price:  د.إ514.17
You save:  د.إ146.91
List Price:  ৳16,408.23
You save:  ৳4,688.40
List Price:  ₹11,637.16
You save:  ₹3,325.14
List Price:  RM657.67
You save:  RM187.92
List Price:  ₦206,269.66
You save:  ₦58,938.40
List Price:  ₨38,916.02
You save:  ₨11,119.65
List Price:  ฿5,127.43
You save:  ฿1,465.08
List Price:  ₺4,505.30
You save:  ₺1,287.32
List Price:  B$723.91
You save:  B$206.84
List Price:  R2,571.91
You save:  R734.88
List Price:  Лв252.38
You save:  Лв72.11
List Price:  ₩190,499.14
You save:  ₩54,432.21
List Price:  ₪515.38
You save:  ₪147.26
List Price:  ₱8,140.48
You save:  ₱2,326.02
List Price:  ¥21,963.82
You save:  ¥6,275.82
List Price:  MX$2,336.09
You save:  MX$667.50
List Price:  QR510.10
You save:  QR145.75
List Price:  P1,900.64
You save:  P543.07
List Price:  KSh18,618.67
You save:  KSh5,320
List Price:  E£6,599.78
You save:  E£1,885.78
List Price:  ብር8,037.80
You save:  ብር2,296.67
List Price:  Kz118,908.34
You save:  Kz33,976.24
List Price:  CLP$126,117.11
You save:  CLP$36,036.03
List Price:  CN¥1,014.10
You save:  CN¥289.76
List Price:  RD$8,237.97
You save:  RD$2,353.87
List Price:  DA18,843.53
You save:  DA5,384.25
List Price:  FJ$311.92
You save:  FJ$89.12
List Price:  Q1,086.64
You save:  Q310.49
List Price:  GY$29,266.81
You save:  GY$8,362.54
ISK kr13,793.62
List Price:  ISK kr19,311.62
You save:  ISK kr5,518
List Price:  DH1,395.33
You save:  DH398.69
List Price:  L2,480.56
You save:  L708.78
List Price:  ден7,930.17
You save:  ден2,265.92
List Price:  MOP$1,125.70
You save:  MOP$321.65
List Price:  N$2,570.90
You save:  N$734.59
List Price:  C$5,148.73
You save:  C$1,471.17
List Price:  रु18,603.56
You save:  रु5,315.68
List Price:  S/522.95
You save:  S/149.42
List Price:  K543.64
You save:  K155.33
List Price:  SAR525.05
You save:  SAR150.02
List Price:  ZK3,733.66
You save:  ZK1,066.83
List Price:  L641.14
You save:  L183.19
List Price:  Kč3,187.01
You save:  Kč910.64
List Price:  Ft49,549.41
You save:  Ft14,157.98
SEK kr1,062.40
List Price:  SEK kr1,487.41
You save:  SEK kr425
List Price:  ARS$124,696.09
You save:  ARS$35,630
List Price:  Bs966.60
You save:  Bs276.19
List Price:  COP$540,370.17
You save:  COP$154,402.50
List Price:  ₡71,720.93
You save:  ₡20,493.15
List Price:  L3,457
You save:  L987.78
List Price:  ₲1,052,195.01
You save:  ₲300,648.62
List Price:  $U5,389.29
You save:  $U1,539.90
List Price:  zł547.82
You save:  zł156.53
Already have an account? Log In


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.

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.