2023-09-04 15:04:51 +02:00
|
|
|
import math
|
|
|
|
import random
|
2023-09-06 13:58:37 +02:00
|
|
|
def intToDotted(ip):
|
2023-09-04 15:04:51 +02:00
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
def printDotted(octets):
|
2023-09-04 15:04:51 +02:00
|
|
|
# input: list containing octets
|
|
|
|
|
|
|
|
# prints out the dotted decimal format
|
|
|
|
# hint: use '.'.join()
|
|
|
|
#returns nothing
|
|
|
|
dottedIP = [str(octets[0]), str(octets[1]), str(octets[2]), str(octets[3])]
|
|
|
|
x = ".".join(dottedIP)
|
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
return x
|
2023-09-04 15:04:51 +02:00
|
|
|
|
|
|
|
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)
|
2023-09-06 14:33:36 +02:00
|
|
|
prefix = random.randint(8, 31)
|
2023-09-06 13:58:37 +02:00
|
|
|
ipandprefix = {"ip":ip, "prefix":prefix}
|
2023-09-04 15:04:51 +02:00
|
|
|
return ipandprefix
|
|
|
|
|
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
def getnetid(ip, prefix):
|
|
|
|
snm=(2**prefix-1)<<(32-prefix)
|
|
|
|
netid = ip&snm
|
|
|
|
return netid
|
2023-09-04 15:04:51 +02:00
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
def analyseSubnet(ip, prefix):
|
2023-09-04 15:04:51 +02:00
|
|
|
# input: ip as int, prefix as int
|
|
|
|
|
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
# prints out net id, broadcast address,
|
2023-09-04 15:04:51 +02:00
|
|
|
|
|
|
|
# and amount of addresses and amount of
|
2023-09-06 13:58:37 +02:00
|
|
|
# usable adresses for a given subnet
|
2023-09-04 15:04:51 +02:00
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
|
|
|
|
wmask = 0xffffffff >> prefix
|
|
|
|
bid = ip|wmask
|
|
|
|
|
|
|
|
hostbits = 32 - prefix
|
|
|
|
hosts = 2**hostbits-2
|
|
|
|
print("Useable Hosts: {}".format(hosts))
|
|
|
|
ids = {"bid":bid}
|
|
|
|
return ids
|
2023-09-04 15:04:51 +02:00
|
|
|
# returns dict: {netid:int, bc:int}
|
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
def newSubnetExercises(ip, prefix):
|
2023-09-04 15:04:51 +02:00
|
|
|
# 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
|
2023-09-06 13:58:37 +02:00
|
|
|
print("Generating new Subnet exercise...")
|
|
|
|
aufgabentyp = random.randint(0,1)
|
|
|
|
bonusaufgabe = random.randint(0,10)
|
2023-09-06 14:33:36 +02:00
|
|
|
newprefix = random.randint(prefix, 32)
|
2023-09-04 15:04:51 +02:00
|
|
|
|
|
|
|
|
2023-09-06 13:58:37 +02:00
|
|
|
|
|
|
|
print(printDotted(intToDotted(ip)))
|
|
|
|
print("Prefix: ", prefix)
|
|
|
|
if aufgabentyp == 0:
|
2023-09-06 14:33:36 +02:00
|
|
|
print("You will have to split the subnet into {} subnets".format(random.randint(2,32-prefix-2)))
|
2023-09-06 13:58:37 +02:00
|
|
|
else:
|
2023-09-06 14:33:36 +02:00
|
|
|
print("Split subnet into subnet with {} Hosts".format(random.randint(8,2**(32-prefix)/2)))
|
2023-09-06 13:58:37 +02:00
|
|
|
|
|
|
|
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(printDotted(intToDotted(nid +i*2**hostbits)))
|
|
|
|
if i > 25:
|
|
|
|
break
|
|
|
|
|
2023-09-04 15:04:51 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-09-06 13:58:37 +02:00
|
|
|
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
|
2023-09-04 15:04:51 +02:00
|
|
|
# 2. wait for user input, noting that the solution
|
|
|
|
# will be shown after after pressing any key
|
|
|
|
# 3. print the solution
|
|
|
|
# 4. exit
|