Compare commits

..

No commits in common. "44d8009f62613c702057d9783ced1f9af9ecfaf2" and "ae2d040c96cb4dc74239e64a93e9f46d1e76ecb1" have entirely different histories.

35
main.py
View File

@ -1,6 +1,5 @@
import math import math
import random import random
def intToDotted(ip): def intToDotted(ip):
# input: ip # input: ip
@ -18,12 +17,13 @@ def intToDotted(ip):
def printDotted(octets): def printDotted(octets):
# input: list containing octets # input: list containing octets
# returns out the dotted decimal format # prints out the dotted decimal format
# hint: use '.'.join() # hint: use '.'.join()
#returns prettyfied DD format as string #returns nothing
dottedIP = [str(octets[0]), str(octets[1]), str(octets[2]), str(octets[3])]
x = ".".join(dottedIP)
return ".".join(str(x) for x in octets) return x
def generateNetwork(): def generateNetwork():
# input: none # input: none
@ -32,13 +32,12 @@ def generateNetwork():
# to avoid getting too large and too small networks # to avoid getting too large and too small networks
# returns dict: {ip:int, prefix:int} # returns dict: {ip:int, prefix:int}
ip = random.randint(0, 2**32-1) ip = random.randint(0, 2**32-1)
prefix = random.randint(8, 31) #max prefix generated is prefix = random.randint(8, 31)
# 30, allowing for at least 1 bit to be subnetted and still have hostbits left
ipandprefix = {"ip":ip, "prefix":prefix} ipandprefix = {"ip":ip, "prefix":prefix}
return ipandprefix return ipandprefix
def getNetId(ip, prefix): def getnetid(ip, prefix):
snm=(2**prefix-1)<<(32-prefix) snm=(2**prefix-1)<<(32-prefix)
netid = ip&snm netid = ip&snm
return netid return netid
@ -52,6 +51,7 @@ def analyseSubnet(ip, prefix):
# and amount of addresses and amount of # and amount of addresses and amount of
# usable adresses for a given subnet # usable adresses for a given subnet
wmask = 0xffffffff >> prefix wmask = 0xffffffff >> prefix
bid = ip|wmask bid = ip|wmask
@ -81,17 +81,19 @@ def newSubnetExercises(ip, prefix):
# returns dict: {ip: int, prefix:int, newprefix:int} # returns dict: {ip: int, prefix:int, newprefix:int}
# newprefx contains the new subnet mask that solves the exercise # newprefx contains the new subnet mask that solves the exercise
print("Generating new Subnet exercise...")
aufgabentyp = random.randint(0,1) aufgabentyp = random.randint(0,1)
bonusaufgabe = random.randint(0,10) bonusaufgabe = random.randint(0,10)
newprefix = random.randint(prefix+1, prefix+5) newprefix = random.randint(prefix, 32)
print("Network: {}/{}".format(printDotted(intToDotted(ip)), prefix))
print("newprefix: {}".format(newprefix))
print(printDotted(intToDotted(ip)))
print("Prefix: ", prefix)
if aufgabentyp == 0: if aufgabentyp == 0:
print("Split the Network into {} subnets".format(2**(newprefix-prefix))) print("You will have to split the subnet into {} subnets".format(random.randint(2,32-prefix-2)))
else: else:
print("Split the network into subnets with {} Hosts".format(2**(32-newprefix))) print("Split subnet into subnet with {} Hosts".format(random.randint(8,2**(32-prefix)/2)))
if bonusaufgabe > 5: if bonusaufgabe > 5:
print("What is the Net ID of the given subnet?") print("What is the Net ID of the given subnet?")
@ -110,15 +112,14 @@ def listSubnets(ip, prefix, newprefix):
# within the given range, showing net id and bc DONE # within the given range, showing net id and bc DONE
# as well as amount of adresses and usable hosts per network DONE # as well as amount of adresses and usable hosts per network DONE
# returns: nothing, this just prints all possible subnets DONE # returns: nothing, this just prints all possible subnets DONE
nid = getNetId(ip, prefix) nid = getnetid(ip, prefix)
print(newprefix) print(newprefix)
hostbits = 32 - newprefix hostbits = 32 - newprefix
newnetbits = newprefix - prefix newnetbits = newprefix - prefix
newnetbits = 2**newnetbits newnetbits = 2**newnetbits
for i in range(0,newnetbits): for i in range(0,newnetbits):
print("subnet", i,":", printDotted(intToDotted(nid +i*2**hostbits))) print(printDotted(intToDotted(nid +i*2**hostbits)))
if i > 25: if i > 25:
print("and many many more...")
break break