Mega Menu

Friday, May 08, 2020

Networks - Subnet Ranges From VPC or Virtual Network Range

How do I compute subnet ranges from a VPC (AWS) or Virtual Network (Azure) IP ranges?

Example:
    If a virtual network or VPC has a IP range of 10.0.0.0/16, and if you would like to provision 256 IPs per subnet,
    you have a potential to create 256 subnets.
            i.e.  Subnet1        10.0.0.1/24
                  Subnet2        10.0.0.2/24
                  Subnet3        10.0.0.3/24
                  Subnet4        10.0.0.4/24 and so on.....

        This is as explained in the other post of ip range computation logic.
        As the VPC range is 2^16 there can be 65536 IPs, which when split into groups of 256,
        there can be 65536/256 = 256 groups i.e. 256 subnets.

        Hope this helps!!


Networks - IP Address Range Computation

How to determine the number of IP addresses available for a given subnet range?
   
Quite common query most of us has and will try to explain a bit on how this can be evaluated.

        Each IP address is a represent as 32 bits, each set of 8 bits separated by a dot (.)

        10.0.0.1 stands for
                        0000 1010.0000 0000.0000 0000.0000 0001

                            Bits are computed by adding values of positions
       POSITIONS       8       7     6      5      4     3      2      1     
       Value                2^7   2^6  2^5  2^4  2^3  2^2  2^1  2^0   i.e.
  Computed Value   128   64    32   16    8      4     2      1
                   
                10 can be formed only when positions 4 and 2 have a value i.e. 0000 1010
                 Few Examples
                        0 = 0000 0000      (0)
                        1 = 0000 0001      (1)
                        2 = 0000 0010      (2)
                        3 = 0000 0011      (2 + 1)
                      40 = 0010 1000      (32 + 8)
                    255 = 1111 1111       (128+64+32+16+8+4+2+1)

 Subnet Range
        Eg: 10.0.0.0/16    (16 is the CIDR Range - Classless Inter-domain Routing)
                This subnet range indicates that, of all the 32 bits of an IP, the last 16 (32-16) bits can be used to define the Host Names, while the first 16 bits are Network Identifiers.

            The range of ips for this subnet would be
                        10.0.(0-255).(0-255) which would be about 2^16 i.e. 65536 IPs.

        Similarly, 10.0.0.0/24 means, the last 8 (32 - 24) bits can be changed to form host names i.e.
                        10.0.0.(0-255), which would be about 2^8 Ips i.e. 256 IPs

                        10.0.0.0/28 means, the last 4 (32 - 28) bits can be changed to form host names i.e.
                        10.0.0.(0-15), which would be about 2^4 Ips i.e. 16 IPs

            An alternate way to calculate the IP count --
                    For a subnet 10.0.1.0/24
                        Total bits = 32
                        Usable = 32 - 24 = 8 (24 is the CIDR range)
                        Total IPs = 2^8 (8 is the usable bit count) = 256

                Example 2: For subnet 10.0.1.0/30
                        Total bits = 32
                        Usable = 32 - 30 = 2 (30 is the CIDR range)
                        Total IPs = 2^2 (2 is the usable bit count) = 4
                                    i.e. 10.0.1.0, 10.0.1.1, 10.0.1.2, 10.0.1.3


        Hope this helps !!!


                        

Monday, May 04, 2020

Python - Import

  1. To import an individual function or class from a module:
    from module_name import object_name
    
  2. To import multiple individual objects from a module:
    from module_name import first_object, second_object
    
  3. To rename a module:
    import module_name as new_name
    
  4. To import an object from a module and rename it:
    from module_name import object_name as new_name
    
  5. To import every object from a module 
    import module_name
    

Python Examples - ListComprehensions, zip and Enumeration

List Comprehensions:

1. Find students scored more than 60 using
scores = {
             "Rick Sanchez": 70,
             "Morty Smith": 35,
             "Summer Smith": 82,
             "Jerry Smith": 23,
             "Beth Smith": 98
          }

passed = [name for name, score in scores.items() if score >= 60]
print(passed)


2. Print multiples of 3
multiples_3 = [x * 3 for x in range(1, 21)]
print(multiples_3)


Zip

1. Merge below lists to print in the format - F:23,677,4

x_coord = [23, 53, 2, -12, 95, 103, 14, -5]
y_coord = [677, 233, 405, 433, 905, 376, 432, 445]
z_coord = [4, 16, -6, -42, 3, -6, 23, -1]
labels = ["F", "J", "A", "Q", "Y", "B", "W", "X"]

points = []
for point in zip(labels, x_coord, y_coord, z_coord):
    points.append("{}: {}, {}, {}".format(*point))

for point in points:
    print(point)
   
   
2. Zip lists to dictionary

cast_names = ["Barney", "Robin", "Ted", "Lily", "Marshall"]
cast_heights = [72, 68, 72, 66, 76]

cast = dict(zip(cast_names, cast_heights))
print(cast)



Enumeration

cast = ["Barney Stinson", "Robin Scherbatsky", "Ted Mosby", "Lily Aldrin", "Marshall Eriksen"]
heights = [72, 68, 72, 66, 76]

for i, character in enumerate(cast):
    cast[i] = character + " " + str(heights[i])

print(cast)   


Dictionary

1. Count Fruits

result = 0
basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']

for object, count in basket_items.items():
   if object in fruits:
       result += count

print("There are {} fruits in the basket.".format(result))


Sunday, May 03, 2020

Python - Boolean Check

Any constant can be represented as a boolean value False if its value is zero of any numeric type or empty as defined below.

    eg:
            if test:
                print("Success")
            else:
                print("Failure")
           
    This prints Success if test is not empty and any value greater than 0 (eg: test=4) and false otherwise