mirror of
https://github.com/VitaAetaerna/SubnetExerciseGenerator.git
synced 2024-09-20 03:10:57 +02:00
137 lines
4.2 KiB
Python
137 lines
4.2 KiB
Python
import math
|
|
import random
|
|
|
|
def intToDotted(ip):
|
|
# input: ip
|
|
|
|
# determins dotted decimal format for given ip as int
|
|
|
|
# returns list: octets
|
|
octets = []
|
|
while ip != 0:
|
|
octets.append(ip % 256)
|
|
ip = math.floor(ip / 256)
|
|
octets.reverse()
|
|
return octets
|
|
|
|
|
|
def printDotted(octets):
|
|
# input: list containing octets
|
|
|
|
# returns out the dotted decimal format
|
|
|
|
# hint: use '.'.join()
|
|
#returns prettyfied DD format as string
|
|
|
|
return ".".join(str(x) for x in octets)
|
|
|
|
def generateNetwork():
|
|
# input: none
|
|
# generates a random ip and prefix
|
|
# the generated prefix should be in the range [8, 30]
|
|
# to avoid getting too large and too small networks
|
|
# returns dict: {ip:int, prefix:int}
|
|
ip = random.randint(0, 2**32-1)
|
|
prefix = random.randint(8, 31) #max prefix generated is
|
|
# 30, allowing for at least 1 bit to be subnetted and still have hostbits left
|
|
ipandprefix = {"ip":ip, "prefix":prefix}
|
|
return ipandprefix
|
|
|
|
|
|
def getNetId(ip, prefix):
|
|
snm=(2**prefix-1)<<(32-prefix)
|
|
netid = ip&snm
|
|
return netid
|
|
|
|
def analyseSubnet(ip, prefix):
|
|
# input: ip as int, prefix as int
|
|
|
|
|
|
# prints out net id, broadcast address,
|
|
|
|
# and amount of addresses and amount of
|
|
# usable adresses for a given subnet
|
|
|
|
wmask = 0xffffffff >> prefix
|
|
bid = ip|wmask
|
|
|
|
hostbits = 32 - prefix
|
|
hosts = 2**hostbits-2
|
|
print("Useable Hosts: {}".format(hosts))
|
|
ids = {"bid":bid}
|
|
return ids
|
|
# returns dict: {netid:int, bc:int}
|
|
|
|
def newSubnetExercises(ip, prefix):
|
|
# input: a subnet, consisting of an ip
|
|
# and a given prefix
|
|
|
|
# generates a new subnet exercise
|
|
# 1. determine the type of exercise
|
|
# types: split into x subnets
|
|
# split into subnets with x hosts
|
|
# 2. print out the given subnet
|
|
# 3. determine if there is optional exercises
|
|
# like determining a specific subnet (start, end), etc
|
|
# think of new types of exercises to add into this
|
|
# output logic
|
|
# 4. print out the exercise:
|
|
# "split the given network into...."
|
|
# depending on the type of exercise
|
|
|
|
# returns dict: {ip: int, prefix:int, newprefix:int}
|
|
# newprefx contains the new subnet mask that solves the exercise
|
|
|
|
aufgabentyp = random.randint(0,1)
|
|
bonusaufgabe = random.randint(0,10)
|
|
newprefix = random.randint(prefix+1, prefix+5)
|
|
|
|
print("Network: {}/{}".format(printDotted(intToDotted(ip)), prefix))
|
|
print("newprefix: {}".format(newprefix))
|
|
if aufgabentyp == 0:
|
|
print("Split the Network into {} subnets".format(2**(newprefix-prefix)))
|
|
else:
|
|
print("Split the network into subnets with {} Hosts".format(2**(32-newprefix)))
|
|
|
|
if bonusaufgabe > 5:
|
|
print("What is the Net ID of the given subnet?")
|
|
print("What is the start and end of the Subnet")
|
|
if bonusaufgabe > 8:
|
|
print("What is the Adress of the next subnet?")
|
|
|
|
subnetdic = {"ip":ip, "prefix":prefix, "newprefix":newprefix}
|
|
return subnetdic
|
|
|
|
def listSubnets(ip, prefix, newprefix):
|
|
# input: a network range and a subnet prefix to split into DONE
|
|
|
|
# note: sanity check with: assert newprefix > prefix DONE
|
|
# this function prints out every possible network ???
|
|
# within the given range, showing net id and bc DONE
|
|
# as well as amount of adresses and usable hosts per network DONE
|
|
# returns: nothing, this just prints all possible subnets DONE
|
|
nid = getNetId(ip, prefix)
|
|
print(newprefix)
|
|
hostbits = 32 - newprefix
|
|
newnetbits = newprefix - prefix
|
|
newnetbits = 2**newnetbits
|
|
for i in range(0,newnetbits):
|
|
print("subnet", i,":", printDotted(intToDotted(nid +i*2**hostbits)))
|
|
if i > 25:
|
|
print("and many many more...")
|
|
break
|
|
|
|
|
|
if __name__ == '__main__':
|
|
ipdic = generateNetwork()
|
|
ip = ipdic.get("ip")
|
|
prefix = ipdic.get("prefix")
|
|
newprefixdic = newSubnetExercises(ip=ip, prefix=prefix)
|
|
if input("Press Space and enter to continue") != "":
|
|
listSubnets(ip, prefix, newprefixdic.get("newprefix"))
|
|
# 1. Generate new exercise
|
|
# 2. wait for user input, noting that the solution
|
|
# will be shown after after pressing any key
|
|
# 3. print the solution
|
|
# 4. exit
|