Demo entry 6783741

Use this code to run VLSM (variable length subnetting)

   

Submitted by Scottie on Feb 20, 2019 at 08:31
Language: Python 3. Code size: 3.0 kB.

import ipaddress

#This is sample program for network admins to design VLSM subneting
#This is version 3.  In this version, instead of storing data in multidimantion arrays
#I will using dictionaries instead
#This version will also use more functions to make the code look cleaner

def display_menu(subnet_count):
    dash= "*"*30
    print(dash)
    print("Use this program to calculate variable length subnetting")
    #print("In this version, we will only take class C addresses")
    
def ask_count():
    while True:
        try:
            subnet_count= int(input ("Please enter the number of desired subnets: "))
            break
        except ValueError:
            print("\nYou have entered an invalid input, please try again")

    print("*"*30)
    print("You wanted " + str(subnet_count) + " subnets")
    print()
    return subnet_count

def ask_address():
    while True:
        try:
            address = input("Please enter your network address: ")
            ip_add = ipaddress.ip_address(address)
            break
        except ValueError:
            print("\nYou have entered an invalid input, please try again")

    #print(ip_add)  #for debug
    return(ip_add)

def add_list_and_subnet(vlsm_list, count):
    count += 1
    print("Please enter the name of subnet",count)
    name= input("Subnet name: ")
    name = name.title()

    while True:
        try:
            vlan= int(input("Please enter the amount of host needed for this subnet: "))
            while ((vlan >254 or vlan <= 1)):
                vlan = int(input("\nPlease enter vlan size between 1 and 254: "))
            break
        except ValueError:
            print("\nYou have entered an invalid input, please try again")



    print(name, 'needed ', vlan, 'hosts')
    print()

    vlsm_list[name] = subnetted(vlan)

def subnetted(hosts):
    """
    Take amount of hosts as input and return the smallest size of vlan
    that can be used for this subnet
    """
    vlan = 2**0

    while (vlan-1) < hosts:
        vlan = vlan*2

    return vlan

def sort_vlsm(vlsm_list):
    temp = sorted(vlsm_list.items(), key=lambda x:x[1], reverse = True)
    vlsm_list.clear()
    vlsm_list = dict(temp)
    return(vlsm_list)
    
def show_result(vlsm_list, ip_add):
    for key, value in vlsm_list.items():
        print('Subnet ', key, ' has network address of ', ip_add,
              '\nwith host addresses from ', ip_add+1, 'to ',ip_add+ vlsm_list[key]-2
              ,'\nwith broadcast address: ', ip_add+vlsm_list[key]-1)
        print()
        ip_add = ip_add + int(value)
        
def main():
    subnet_count = 0
    vlsm_list = {}
    
    display_menu(0)
    subnet_count = ask_count()
    ip_add = ask_address()
    
    for i in range(0,subnet_count):
        add_list_and_subnet(vlsm_list,i)

    vlsm_list=sort_vlsm(vlsm_list)
    show_result(vlsm_list, ip_add)
    
if __name__ == "__main__":
    main()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).