import csv import ipaddress # Here we define main menu. # MainMenu function does two things. prints the main menu text and calls function for each menu option errormessage = 'ERROR: Invalid option was chosen. Try again' + '\n' invalidIP = 'ERROR: Invalid IPv4 address detected >>>' invalidMask = 'ERROR: Invalid subnet mask detected >>>' def MainMenu(): mainmenu = ['|======== Cisco ASA =========|', '| 1: Add Host Objects |' , '| 2: Add Subnet Objects |', '| 3: Add TCP Service Object |' , '| 4: Add UDP Service Object |', '| 5: Exit |', '|----------------------------|'] for each_option in range (0,len(mainmenu)): print(mainmenu[each_option]) while True: try: CHOICE = int(input('Choose The Option: ')) if CHOICE == 1: Host_Objects() MainMenu() break elif CHOICE == 2: Subnet_Objects() MainMenu() break elif CHOICE == 5: break else: print (errormessage) MainMenu() break except ValueError: print (errormessage) MainMenu() # Here we define function that will be called in MainMenu if user selects "1" def Host_Objects(): with open('ObjectList.csv') as csvfile: CSV_READ = csv.reader(csvfile, delimiter = ',') next(CSV_READ) for each_object in CSV_READ: objectname = each_object[0] objectip = each_object[1] try: validate = ipaddress.ip_address(objectip) print('object network ' + objectname, '\n', 'host ' + str(validate)) except ValueError: print(invalidIP, objectip) # Here we define function that will be called in MainMenu if user selects "2" def Subnet_Objects(): with open('ObjectList.csv') as csvfile: CSV_READ = csv.reader(csvfile) next(CSV_READ) for each_object in CSV_READ: objectip = each_object[1] objectmask = each_object[2] print ('object network ' + objectip, '\n', 'subnet ', objectip + ' ' + objectmask) MainMenu()