Checking IP Address Validity

Python 3: Automating Your Job Tasks Superhero Level: Automate Network Tasks with Python 3
12 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

Now it's time to determine if each of the IP addresses in the first text file is valid or not, meaning it follows the correct format and is not a reserved IP address. There are many reserved IP addresses. And you can find all of them right here on Wikipedia by following this link which is also attached to this lecture by the way, however, we will handle some of the most common ones in our code, meaning we will make sure that the IP addresses in our file are not loopback multicast broadcast link local or addresses reserved for future use, meaning they don't start with 127 2242239 all to five fives 169 dot 254 or 242 to five five. For this we should define the conditions for an IP to be valid. Of course using an if statement. This statement will give valuate multiple conditions simultaneously, because we want our IP addresses to meet all of these conditions at the same time.

Otherwise, if at least one of the conditions is evaluated as false, the entire if statement becomes false, because we are using the end logical operator between the conditions. And in this case, the L statement kicks in and prints a message to the screen and then quit the program. Also, before moving on to analyze the conditions, notice that we used a function called ip addr valid, which will later be called in the main application from within this module. Inside this function, we are using a for loop to iterate over the list of IP addresses that we got after reading the file and creating a list in the previous lecture. This is the for loop right here. This list is taken by the function as a parameter, as you can see right here in between its parentheses and you will see this in action.

When all of these modules, we will imported and used inside the main application. So for each IP address in the file for IP enlist, we are performing the same tests regarding the validity of the IP address. Now, let's consider that the for loop extracts the first IP address, since each addressing the file was placed on a separate line, when reading the file and creating the list using the read lines method, the IP addresses will each be followed by a backslash n a newline character. So using the our strip method right here, which is a string specific method that strips a character from the right side of a string, we are getting rid of the backslash n and keeping just the IP address itself. Actually, let me prove this to you. I will open up the Python interpreter and then open the IP dot txt file.

Okay, so let's open the file using the open function. This is the path to my file and we are going to open it for reading. Now using the read lines method. Let's create the list of IP addresses. So a dot read lines returns this list right here. Notice that the first two IP addresses in the list have that annoying backslash n at the end.

Now let's strip that away using a similar for loop. So for i in a dot read lines, colon, i dot, our strip of backslash n. Notice we didn't get any result Why? Because we already went through the file. So always remember to use the seek method. So a dot seek of zero. And now let's run our for loop again.

Great, we got rid of the backslash ends. So that's exactly what we're doing inside our module as well. So I hope it's all clear now. Now let's get back to the code. And notice that we're also splitting each IP address in the list using the.as, a delimiter, and then saving the list generated by the split method using the octet list variable. Why?

Because we want to Extract each of the four octets of each IP address and analyze them by checking the conditions in the if statement below. So going back to the Python interpreter, let's take the first IP address in the list and extract its octets in the form of a list using the split method, first of all, a dot seek of zero. And now let's just use a dot read lines of zero. Because we're interested in the first element in the list, dot, our strip of backslash n, we're stripping the backslash. And once again, that split and we're using the dots as a delimiter. Sorry, my mistake, I typed in red line instead of red lines.

So let me try this again. A dot seek of zero and a dot red lines instead of red line. Okay, first of all, notice a very cool thing here. We chained and applied to methods on a string at the same time. So we have the list general By red lines right here, then we only care about the first element in that list. So we use the index zero, then we write strip the newline character right here.

And finally, we split that IP address into four octets. Using the.as, a delimiter. The result is those octets being the elements of the list. The same is done for each IP address in the file, then it goes through the conditions specified inside the if statement. And now it's time to see what should those conditions be? Well, let's think of a valid non reserved IP address.

First, it should have four octets. Right, this is obvious. So our list of octet stored in the variable octet list should have four elements. This means Len of octet list equals equals four. This is the first condition. Now let's focus on the value of the first octet.

First, notice that each element of octet list of this list, as you can see right here is a string. That's why inside our if statement, we have to convert it to an integer using the INT function for comparing it to other values. Otherwise, we wouldn't be able to compare it with any integers. Now we know that an IP address starting with two to four, or higher octet is a multicast or a broadcast address, or one which is reserved for future use. That would be Class D or Class C network for all your gigs out there. Since we're interested in unicast, addresses only, we're going to specify in the same if statement, that octet list of zero so the first octet in our list should be between one and two to three inclusively.

And this is the condition right here, octet list, less than or equal to two, three, and at the same time, greater than or equal to one We also know that in the unicast range, we have two special cases. First, the addresses starting with 127 are reserved for loopback interfaces, so we must exclude them as well. That's why the third condition is that octet list of zero is different than 127. The second special case is any address starting with 169 dot 254. These are auto assigned by Windows machines to interfaces configured for DHCP that are unable to contact a DHCP server. So we should add this condition as well.

This is the condition right here. Now as a side note, notice that I used a pair of parentheses for each condition. I did this for better readability. We have multiple conditions inside this if statement. So we must be able to read them as easy as possible. Also, I remind you that we have a logical AND operator between conditions.

This means that the IP address entered by the user should meet all of these conditions at the same time in order to be considered valid, because remember, when we talked about logical operators, I said that when using end, all operations should be true in order for the expression to be evaluated as true as a whole. So this if statement right here says, if the octet in your IP address meet all of these conditions right here, then it is a valid IP address, and the program should continue evaluating the next iteration, the next IP address, otherwise, a message is printed out and the execution stops, as you can see under the else clause. Now back to our 169 to five for condition, and this is it right here. Notice that this condition is made of two other smaller conditions, meaning the first octet should not be 169.

And the second octet should not be 254. But Why did I use the OR operator in between them? At first sight, you would be tempted to say that there should be an end logical operator, they're not an OR operator. If I would have used and between them, then I would have excluded some valid addresses to like, for example, 169 dot 200 dot one dot one, or 10 dot 254 dot one dot one, because the end operator would have told Python that both operands should be true at the same time. So 169 and 254 would have been totally forbidden inside an IP address, which is not the case because we only care about the unique combination of 169 dot 254. Let me prove this to you.

And then we will move on to the next module. So opening up the Python interpreter, let's consider a to be this IP address. And let's see we have octet list equals a dot split using the.as a delimiter. Now, let's see At least Okay, now let's use the same conditions and see how the if else block behaves both when using the end operator. And when using the old logical operator between our two smaller conditions. So let me paste in a similar code.

So inside the if statement, we have the exact same conditions. And for the condition that we're discussing right now, this one right here, we still have the or logical operator between the two smaller conditions. So if the IP address is valid, then we print valid to the screen and we print invalid. So notice that in this case, our IP address that we just defined is a valid IP address, since we have 168 dot 254, not 169 dot 254. So this is not a link local address. This means that our code right here should return valid right?

And it did it does. Now let's change the code The if statement. Replace or with and between the two smaller conditions. Okay, so instead of all we type in and and now using the same IP address, let's run the code again. And this time we have invalid. So in this case, notice that using the end operator will result in a valid IP address being evaluated as invalid.

Since now, 169 and 254 are both individually completely forbidden in the first and second octet, respectively. And of course, that's not what we're looking to achieve. We want to cover only the case when the combination of the two, namely 169 254, is in the first two octets of the IP address. Now, if we change our IP address to a link local one using 169, and 254, and then use the OR operator instead of end, let's see what happens. Their behavior should be the normal one. So the results should be invalid, right?

Because this time, we truly have a reserved IP address. So let's redefine a, and enter an invalid or reserved IP address 169 dot 254 dot two dot two. And now using the OR operator as we do within our code, let's run this if else block again. And we still got valid because I forgot to also split the address. So now let's see octet list, the new octet list list. Okay, now if we run the code once again, using the OR operator in between the two sub conditions, we are getting indeed invalid, because now we have a link local IP address, which cannot be considered valid for the purpose of our application.

Okay, so great. This proves my point. I'll see you in the next lecture to discuss yet another module for our application.

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.