This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Pages

This blog includes subject like Computer Organization, Microprocessor, Digital Electronics, System Programming

Powered by Blogger.

Sunday, August 18, 2019

Write a program to demonstrate sub-netting and find the sub-net masks.


//Write a program in Python to demonstrate sub-netting and find the sub-net masks.

import sys
# Get address string and CIDR string from command line
(addrString, cidrString) = sys.argv[1].split('/')

# Split address into octets and turn CIDR into int
addr = addrString.split('.')
cidr = int(cidrString)

# Initialize the netmask and calculate based on CIDR mask
mask = [0, 0, 0, 0]
for i in range(cidr):
mask[i/8] = mask[i/8] + (1 << (7 - i % 8))

#print "mask: ", mask

# Initialize net and binary and netmask with addr to get network
net = []
for i in range(4):
net.append(int(addr[i]) & mask[i])

#print "net : " , net

# Duplicate net into broad array, gather host bits, and generate broadcast
broad = list(net)
brange = 32 - cidr
for i in range(brange):
broad[3 - i/8] = broad[3 - i/8] + (1 << (i % 8))

#print "broad: ", broad

# Print information, mapping integer lists to strings for easy printing
print "Address:   " , addrString
print "Netmask:   " , ".".join(map(str, mask))
print "Network:   " , ".".join(map(str, net))
print "Broadcast: " , ".".join(map(str, broad))


//Here is sample for try1.py

net = [200, 100, 33, 64]
broad = list(net)
brange = 32 - 23
for i in range(brange):
broad[3 - i/8] = broad[3 - i/8] + (1 << (i % 8))

print "broad: ", broad


0 comments:

Post a Comment