Random IP Address Generation Algorithm

Python 3: Automating Network Tasks Network Application #2 - Building an Interactive Subnet Calculator
8 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
€93.07
List Price:  €130.30
You save:  €37.23
£79.62
List Price:  £111.47
You save:  £31.85
CA$136.60
List Price:  CA$191.24
You save:  CA$54.64
A$151.87
List Price:  A$212.63
You save:  A$60.75
S$135.20
List Price:  S$189.29
You save:  S$54.08
HK$781.33
List Price:  HK$1,093.90
You save:  HK$312.56
CHF 90.72
List Price:  CHF 127.01
You save:  CHF 36.29
NOK kr1,092.46
List Price:  NOK kr1,529.50
You save:  NOK kr437.03
DKK kr694.20
List Price:  DKK kr971.90
You save:  DKK kr277.70
NZ$167.15
List Price:  NZ$234.02
You save:  NZ$66.86
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,946.16
List Price:  ৳15,325.07
You save:  ৳4,378.90
₹8,342.71
List Price:  ₹11,680.13
You save:  ₹3,337.42
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,679.53
List Price:  ฿5,151.49
You save:  ฿1,471.96
₺3,232.18
List Price:  ₺4,525.18
You save:  ₺1,293
B$511.10
List Price:  B$715.57
You save:  B$204.46
R1,851.52
List Price:  R2,592.21
You save:  R740.68
Лв181.98
List Price:  Лв254.78
You save:  Лв72.80
₩135,881.87
List Price:  ₩190,240.05
You save:  ₩54,358.18
₪371.60
List Price:  ₪520.25
You save:  ₪148.65
₱5,713.97
List Price:  ₱7,999.79
You save:  ₱2,285.82
¥15,308.41
List Price:  ¥21,432.39
You save:  ¥6,123.98
MX$1,693.57
List Price:  MX$2,371.07
You save:  MX$677.49
QR364.01
List Price:  QR509.63
You save:  QR145.62
P1,365.33
List Price:  P1,911.52
You save:  P546.18
KSh13,468.65
List Price:  KSh18,856.65
You save:  KSh5,388
E£4,794.02
List Price:  E£6,711.82
You save:  E£1,917.80
ብር5,699.43
List Price:  ብር7,979.43
You save:  ብር2,280
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$94,755.52
List Price:  CLP$132,661.52
You save:  CLP$37,906
CN¥723.62
List Price:  CN¥1,013.10
You save:  CN¥289.48
RD$5,785.38
List Price:  RD$8,099.76
You save:  RD$2,314.38
DA13,457.99
List Price:  DA18,841.73
You save:  DA5,383.73
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.21
List Price:  Q1,085.33
You save:  Q310.11
GY$20,876.73
List Price:  GY$29,228.26
You save:  GY$8,351.52
ISK kr13,989.60
List Price:  ISK kr19,586
You save:  ISK kr5,596.40
DH1,008.69
List Price:  DH1,412.20
You save:  DH403.51
L1,771.31
List Price:  L2,479.90
You save:  L708.59
ден5,730.13
List Price:  ден8,022.42
You save:  ден2,292.28
MOP$803.11
List Price:  MOP$1,124.38
You save:  MOP$321.27
N$1,860.85
List Price:  N$2,605.27
You save:  N$744.41
C$3,673.63
List Price:  C$5,143.23
You save:  C$1,469.60
रु13,311.40
List Price:  रु18,636.50
You save:  रु5,325.09
S/375.46
List Price:  S/525.66
You save:  S/150.20
K383.63
List Price:  K537.10
You save:  K153.47
SAR375.03
List Price:  SAR525.05
You save:  SAR150.02
ZK2,685.42
List Price:  ZK3,759.69
You save:  ZK1,074.27
L463.14
List Price:  L648.42
You save:  L185.27
Kč2,328.17
List Price:  Kč3,259.54
You save:  Kč931.36
Ft36,248.95
List Price:  Ft50,749.98
You save:  Ft14,501.03
SEK kr1,083.98
List Price:  SEK kr1,517.62
You save:  SEK kr433.63
ARS$87,816.53
List Price:  ARS$122,946.66
You save:  ARS$35,130.12
Bs689.12
List Price:  Bs964.79
You save:  Bs275.67
COP$389,940.87
List Price:  COP$545,932.82
You save:  COP$155,991.94
₡50,893.45
List Price:  ₡71,252.86
You save:  ₡20,359.41
L2,475.75
List Price:  L3,466.15
You save:  L990.40
₲747,391.81
List Price:  ₲1,046,378.43
You save:  ₲298,986.62
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł402.67
List Price:  zł563.75
You save:  zł161.08
Already have an account? Log In

Transcript

In this final part of our application Part Four, we are going to take care of the random IP address generation algorithm. Because we want to allow the user to generate several random IP addresses from the subnet. I've used a while loop while true right here that keeps asking the user if he wants a new IP address from that subnet to be generated after generating the previous one. As long as he answers why for Yes, the code inside the if block will be executed, and those new addresses will be generated. Of course, as we've seen before, to implement such a behavior, we need a continuous statement, as you can see right here, that brings the user back to the top of the while loop at the point where he is asked to input Y for yes or no for No. So after the while true loop and also the input function, asking the user for input We need an IF block.

So if the user enters why, then the code under the if clause gets executed. Otherwise, inside the else clause, the break statement interrupts the while loop and the program is terminated. Now let's focus on the code under the if clause. First of all, notice right here that we have a four loop nested inside another for loop. And each of these loops refers to one of the lists we've seen earlier, namely, BSD IP address and net IP address. So this list right here, and this list right here, let's get back to our four loops nested for loops.

Now let's go ahead and see those lists again by uncommenting. The corresponding print functions in order to have a very clear picture of what we're about to do. So I'm going to uncomment this and this print function save. Let's run the application. So by THON the Network apps subnet calculator, Python three.pi 192 168 dot 10 dot 1255255255 dot zero. Okay, so these are the lists right here.

Okay, so I must have on commented the wrong print function. Yes, I should have uncomment this one right here. Okay, save the file again, let's run the application 192 168 dot 10 dot 1255255255 dot zero. Okay, great. So these two lists right here, these lists contain the octet of the network address and the broadcast address respectively. Now let's talk a bit about the enumerate function that you're seeing right here in the code.

Keep in mind that enumerate returns both the index of one element and the value of that element at the same time. In order to see that let's uncomment the print function inside the First for loop. So I'm going to do this. And I'm going to run the application once again. 192 168 10 dot one. Okay, let's press Y.

So as you can see right here, we got index zero for the first octet in the broadcast address, which is 192, then index one for the second octet, and so on. Good. Now, let's check the same thing using the second print function. So I'm going to comment this print function right here. And I'm going to uncomment the function on the nested for loop, save the file. And now let's run it once again, to see the results.

Hit Yes, this time, this loop being a nested for loop, it will be executed each time the first loop iterates over the broadcast address of that list. So total of four iterations one for each element of the list. That's why we get the network address index value pair four times. Okay, great. Now Let's get back to the code. And let's focus on this piece of code.

So up until this point, we iterated over the elements of both lists. Now let's see the logic behind this part of our application. Inside the nested for loop, we have an IF block nested inside another if block. So as soon as the program iterates over the elements of both lists, and learns their indexes and values, we are inserting some conditions. Now let me explain the logic within this nested if else code block for the elements at index zero in both lists. So for equal indexes, that's the roll of the first if statement right here.

If the values of those elements are equal, this is the role of the second if statement or the nested if statement. Then append that value, as you can see right here to the empty list we created up here called generated IP. By doing this, we are actually appending the network part of the IP address To the list, so that would be the common octet of the network and broadcast addresses. Otherwise, if the octet at a particular index in the first list is different than the octet at the same index in the second list, then the program executes the lines of code indented under the else clause. First of all, let's focus on the code inside the parentheses. So this code right here, I've used the module called random, which is a Python built in module.

And from that module, I've used the RAND int method. What this method does is it generates a random integer between the values given as arguments inclusively. So if you have random dot random of one comma 10. Actually, let's try this right into the Python interpreter. So import random, and let's say we have random dot Rand int of one comma 10. This line of code may generate any number between In one and 10, including the boundary values one and 10.

In our case, we want a random integer to be generated for each octet representing the host part of the IP address. This number cannot be 300, for example, which would be an invalid value for an IP address octet, we must obtain valid octets as well as random IP addresses anywhere in between the network IP address and the broadcast IP address. So inside the subnet, finally, after the random octet has been generated, it should be converted to a string and added to the same list called generated IP. So this is the role of the append method and the str function. We are appending the random octet to this list in order to join the elements of this list into a single IP address. That will actually be the final random address that we need.

And of course, after joining the octet inside this list together, we should print that random IP address to the screen. So this is the join method right here using the.as, a delimiter. Of course, as for any IP address, and finally we have print random IP address is, and we have a string format operator and the IP address itself. So let's uncomment these two print functions right here, in order to see the list of octets. And finally, the random IP address. I'm going to save the file.

And now let's run the application. Once again, 192 168 dot 10 dot 1255255255 dot zero, I'm going to hit y. And indeed, this is the list. And this is the random IP address itself after joining the elements of the list, also, let's generate a couple of other random IP addresses just to prove that this works. So I'm going to hit Y for yes. And this time you can see that another random IP address has been generated dot 10 dot six.

We previously had dot 10 dot 42 only So let's try it once again. And this time we have dot 10 dot 104. And we could do this on and on until we get the number of random IP addresses that we need. Of course going back to the code, the last thing we should do in our application is called the subnet calc function. And now we are ready to do some testing in the next lecture. 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.