I am a network engineer and trying to write Cisco Event Manager Applet scripts by hand was a huge hassle so I pieced together a script to try and make it easier/automated-ish.
Reason For Program:
When you use the program it outputs your script to the terminal and creates a file for your records. I created an initial prompt to ask if you have ever used this program before so that it will completely erase the file before beginning. The reason for this was to remove any chance of data getting mixed up and potentially causing issues with the configuration changes you are trying to make.
Problem:
If users say "yes" to the initial prompt asking if they have used the program before and Python can't find the file it errors out.
Assistance Needed:
I want to try to have the program just continue if the file is not found rather than aborting.
(Any suggestions on cleaning this up would be helpful too if anyone has any)
Script:
# Used for creating, editing, erasing, reading files #
import os
# First While loop counter #
beg = 0
# Second While loop counter #
counter0 = 0
# Nested While loop counter #
counter1 = 0
# Used to start CLI action numbering at the correct point #
line = 3
# Searches for any previously made EMA script file and removes it. Currently errors out if file isn't seen #
while beg == 0:
remove = input("Have you used this program before?\n[Y] Yes\n[N] No\n: ").lower()
if remove == "y":
os.remove("EMA_Script.txt")
print("File Deleted")
beg += 1
elif remove == "n":
beg += 1
else:
print("Incorrect input.")
beg += 0
# Begins asking for CLI commands and writing inputs to file in sequence #
while counter0 == 0:
name = input("\nName your applet: ")
hour = int(input("\nAt what hour do you want this to run (24h clock)?: "))
minute = int(input("\nAt what minute on the hour do you want this to run?: "))
f = open("EMA_Script.txt", "a+")
f.write('event manager applet {}\nevent time cron cron-entry "{} {} * * *"'.format(name, minute, hour))
f.write('\naction 1.0 cli command "enable"\naction 2.0 cli command "conf t"')
f.close()
while counter1 == 0:
print("\nBegin Command section.")
action = input("\nCommand: ")
forward = input("\nEnter 1 to add more or 2 if you are finished: ")
if forward == "1":
f = open("EMA_Script.txt", "a+")
f.write('\naction {}.0 cli command "{}"'.format(line, action))
f.close()
line += 1
counter1 += 0
elif forward == "2":
f = open("EMA_Script.txt", "a+")
f.write('\naction {}.0 cli command "{}"'.format(line, action))
f.close()
line += 1
f = open("EMA_Script.txt", "a+")
f.write('\naction {}.0 cli command "end"'.format(line))
f.close()
f = open('EMA_Script.txt', 'r')
print(f.read())
f.close()
counter1 += 1
else:
print("Incorrect input. Re-input previous command!")
counter1 += 0
break
try-exceptor better 'try' -with-exceptto suppress errors.