Converting to Binary. Calculate Hosts per Subnet. Wildcard Masks

Python 3: Automating Network Tasks Network Application #2 - Building an Interactive Subnet Calculator
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.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, let's continue our discussion from the previous lecture. According to our plan, in this part part two of our application, we are going to see how to convert the subnet mask to binary, calculate the number of hosts per subnet, and also determine the wildcard mask. So let's get to work and study the code. The first thing I did here is I have created an empty list called mask octets binary. This is where each octet of the subnet mask entered by the user will be stored as an element in binary format after the conversion has been made. The next thing to do is to iterate over the list of subnet mask octet that we previously used up here.

So mask octets, this list right here, okay, in this list, the octets are represented in decimal format, meaning 255, or 252, for instance, now it's time to work on each of these octets and convert each of them to binary Then add them to the newly created list called mask octet binary. So for each octave in the list, we are going to create a variable called binary octet. The value of this variable is given by the conversion of each element in the mask octet list from string as it currently is as a result of using the spit method on the string entered by the user to an integer and then from integer to binary format. Let's open up the Python interpreter to understand this operation better. So let's consider one of the elements of the list which is an octet of the subnet mask as a string.

So let's say a equals to five, five. Now we want this string to be converted to binary. However, we are not allowed to convert a string straight to a binary without having it converted to an integer. First, let me prove this to you. So let's try been of a you can see that the Python exception itself provides a hint on how to solve this issue. So let's convert our string to integer first and then convert that integer to binary.

So I'm going to use int of A, which is 255. And now let's apply the bin function to the result of the INT function applied on our string. So bin of int of a, enter. Okay, great. So this is the way we are converting each of the octets inside the subnet mask to binary. Also, this is the way Python displays binary values by prepending them with zero and B.

Now, since I want to get rid of this zero be at the beginning of this value, I'm going to use the L strip method on each octet. As you can see right here in the code, l strip of zero B, which strips away the character or the set of characters that we provide as an argument from the beginning the left side of a string. Let's try this on our binary octet. So let's return to the Python interpreter and let's use being of int of dot L strip. And let's use zero B as an argument in between quotes, enter. Okay, great.

That's exactly what we wanted to achieve the binary format of a subnet mask octet without the zero be prepending the value. Okay, now going back to our application, we are performing these operations on each of the octet in the mask octets list. And then we append each of these octets that have been converted to binary to the mask octet binary list using the append method. But notice one more thing here. As we are appending each result to the list. I have also decided to use the Z field method, which may be a method you haven't encountered yet.

Let me explain why we need this method. We've just seen an example in the Python interpreter of converting the 245 octet to binary. If we consider another octet, let's say 252 and apply the same operations. Let's find out the result. So let me redefine a equals two to five to been have a will, of course resulting on error. So we would have to use being of int of a.

And you can notice yet again that we get eight bits for each of these binary octets. However, what if we have zero as an octet of our subnet mask, let's test this in the Python interpreter as well. So I will redefine a yet again, a equals zero, and now been an interview. This time, we only get a single bit instead of eight bits as we did before, but that's not what we want. We want eight bits in every octet in order to join them together into a 32 bit binary mask. This is why I use the Z field method.

So if the length of each binary octet is less than eight bits, then we should feel that octet with zeros until the desired length is reached. Otherwise, if the octet is already a bit longer than the simple method will have no effect since the desired length is already already achieved. Now returning to our application after the filling operation, also called padding is complete. For each of these octets, we simply append the octet to the mask, octet binary list. And that's it. Now using this for loop right here, let's test our code on a subnet mask inside the Python interpreter.

So let's say we have a equals the following subnet mask 255255252 dot zero. Now let's create the list of octets. So mask octets equals a dot split and we will use the.as a delimiter. Sorry, I misspelled the name of the method. Okay. Now let's see this list mask octets.

Okay, great. Now let's also create our empty list. mask octets binary equals open and close square brackets. And now let's use the exact same for loop to obtain the final list of points. octets for this, I'm just going to copy and paste this for loop right into the Python interpreter. Did I miss something here?

Yeah, indeed I did. I forgot to also include the appending operation inside the for loop. So let me copy and paste this one more time. And now let's see the mask octets binary list. Nice. So at this point, we have the list of subnet mask octets in binary format.

Finally, let's join the elements of this list into a single string having no separator so nothing in between the double quotes when using the join method, by joining the elements of this list, we are going to obtain the subnet mask that the user has entered at the prompt in binary format. So in order to do that binary, underscore mask equals opening close double quotes dot now we are using the join method and we are joining the elements of mask octets binary Now let's see binary underscore mask. So this is what the subnet mask looks like in binary format. This is going to help us calculate other parameters further into the application. Let's return to our code now. So we have performed these operations right here up to this point.

Next, you know that the number of hosts per subnet can be calculated using the formula two to the power of x, where x is the number of hosts bits minus two. These two are the network address and the broadcast address of the subnet. Knowing this, let's see how to get the number of hosts. First, let's count how many zeros or host bits are there in the subnet mask entered by the user. For this, we are going to use the binary mask value that we have just calculated and the count method so we will have number of zeros equals binary mask. So let's say this mask right here, count of zero, we are counting the number of zeros inside the subnet mask.

Also the number of ones inside the subnet mask can be easily found by subtracting the number of zeros from the total number of bits, which is 32. So we have number of ones equals 32 minus number of zeros. Finally, to get the number of hosts, we just have to apply the formula. So we have two to the power of number of zeros minus two. Also notice that I passed this formula as an argument to the ABS function, also known as absolute, which returns the absolute value for a number. So a positive number.

Even if we have a negative number in between parentheses. I did this to cover the case in which the subnet mask would be a slash 32. So all ones in binary, that would lead us to having two to the power of zero, which is one minus two will return a final result of minus one. And the ABS function takes care of these cases well by returning the absolute value of minus one now, let's Go ahead and see these values for the 255255252 dot zero subnet mask that we used earlier in the Python interpreter. So we have number of zeros equals binary mask dot count of zero number of ones equals 32 minus number of zeros. And finally, we have number of hosts equals a BS of two to the power of number of zeros, minus two.

Now let's see the values. So we have number of zeros to n, number of ones 22. And finally, the number of hosts which is 1022. So for a mask of slash 22, we have 10 binary zeros, 22 binary ones, and 1022 hosts per subnet. Now the last objective of this lecture is to determine the wildcard mask based on the subnet mask. The wildcard mask is actually an inverted subnet mask and can be obtained by subtracting the subnet mask from 255 dot 255 dot 255 dot 255.

The way to do this in Python is by first creating an empty list. So let me scroll down a bit. This is the empty list right here, wildcard octets. This list will store the octet in a string format after each of them is going to be subtracted from 255. Next using a for loop this for loop right here we are iterating over the list of subnet mask octet, and we will create a variable called Wild octet that stores the result of subtracting each octet of the subnet mask from 255. And then we will append this result to our wildcard octet list.

Of course, you should make sure that when the subtraction is performed, each octet in the mask octet list is converted to an integer and then the result Have the subtraction should be converted back to a string before appending it to the wildcard octet list by using the str function. We're doing this because we want to join these octets together and obtain the final wildcard mask. Now let's test this in the Python interpreter and see the list printed out to the screen. So first, I'm going to copy and paste this line of code in order to create the empty list. Next, I'm going to copy and paste the for loop. And finally, let's see the list.

So wildcard underscore octets indeed, returns the octet of our wildcard mask, which as I said, are the result of subtracting each octet of the subnet mask from 245. Of course, the last thing to do is to join the elements of this list into a single string, and then store that string using a variable for further reference. So in order to do that, let's create a new variable wildcard mask equals Open and close double quotes, dot join. And we are joining the elements of this list right here. wildcard octet, so wildcard underscore octets. Now let's see our wildcard mask.

Oops, my mistake here, I'm going to add a.as a delimiter. Because this is the correct format of wildcard mask, let's see the wildcard again. And this time it is correct. Okay, so this was part two of our application. In the next video, we are going to analyze the code for part three. So I'll see you soon.

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.