def getSettings(): settings = parameters.get() com = settings['com'] if com: return(com) else: return(False)
def load(console, vehicle): print("Loading config to running-config") name = vehicle[0].lower() params = parameters.get() conf_file = '{}{}{}.txt'.format(params['prefix'], name, params['suffix']) while True: prompt = read.serial(console) if prompt: if prompt[0][-1:] == "#": write.serial(console, "copy flash:{} run".format(conf_file)) prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') time.sleep(2) prompt = read.serial(console) write.serial(console, '\r') time.sleep(2) prompt = read.serial(console) write.serial(console, 'wr mem') time.sleep(1) prompt = read.serial(console) write.serial(console, '\r') time.sleep(4) prompt = read.serial(console) print("Configured as {}".format(name)) return (True) else: write.serial(console, "\r")
def command(console): ''' Pings command gateway ''' params = parameters.get() failedPing = 0 while True: prompt = read.serial(console) test = params['test'] if prompt: if prompt[0][-1:] == '#' or prompt[0][-1:] == '>': write.serial(console, "ping {}".format(test)) prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') time.sleep(5) prompt = read.serial(console) if any('!!!!!' in s for s in prompt): print("Ping OK") return(True) if prompt[0] == '.': failedPing += 1 if failedPing == 5: failedPing = 0 print("Ping Timeout") tryAgain = input("Try again (y) or ignore (n): ") if tryAgain == "y": continue else: return(True) else: write.serial(console, "\r") time.sleep(1)
def get(): check() params = parameters.get() while True: no_match = True name = input("Enter device name (e.g. DT201): ") if params['case'] == 'lower': hostname = '{}{}{}'.format(params['prefix'], name, params['suffix']).lower() elif params['case'] == 'upper': hostname = '{}{}{}'.format(params['prefix'], name, params['suffix']).upper() with open('./lists/master.csv', 'r') as f: for line in f: stripped = line.strip() split = stripped.split(',') if split[0].lower() == name.lower(): print('-------- Matched --------') print(' Name: {}'.format(split[0])) print('Hostname: {}'.format(hostname)) print(' IP: {}'.format(split[1])) print('-------------------------') user_input = input('Is this correct? (y/n): ') while True: if user_input == 'y': return (split) elif user_input == 'n': print("Try again...") no_match = False break else: user_input = input("Enter 'y' or 'n': ") if no_match: print("No match found in master.csv") appendage = input( "Would you like to append (y) or try again (n): ") if appendage == 'y': ip = input("Enter IP address: ") print("------------------") print(" Name: {}".format(name)) print("Hostname: {}".format(hostname)) print(" IP: {}".format(ip)) print("------------------") check_append = input("You good bro? (y/n): ") if check_append == 'y': append(name, ip) print("Appended {} to master.csv".format(name)) return (name, ip) if check_append == 'n': break else: print("You gone and stuffed up, starting again...") break if appendage == 'n': input("Fix up the spreadsheet and press ENTER to try again") else: print("Sort your shit out, starting again...")
def copy(console, vehicle): params = parameters.get() name = vehicle[0].lower() conf_file = '{}{}{}.txt'.format(params['prefix'], name, params['suffix']) waiting = False while True: prompt = read.serial(console) if prompt: if prompt[0][-1:] == "#": write.serial( console, "copy tftp://{}/config/{} flash:".format( params['ip'], conf_file)) time.sleep(1) prompt = read.serial(console) write.serial(console, "\r") time.sleep(3) prompt = read.serial(console) elif prompt[0][:9] == 'Accessing': print("waiting...") waiting = True prompt = read.serial(console) time.sleep(4) prompt = read.serial(console) elif any("(Timed out)" in s for s in prompt): if prompt[0][-11:] == '(Timed out)': while True: try_again = input("TFTP Timed out, try again? (y/n): ") if try_again == 'y': break elif try_again == 'n': print("Aborting...") return (False) else: print("I said y or n") elif any("[OK - " in s for s in prompt): print("OK Detected") waiting = False return (True) elif any("Do you want to over write? [confirm]" in s for s in prompt): print("Overwriting") write.serial(console, "\r") time.sleep(1) return (True) else: write.serial(console, "\r") time.sleep(1)
def exists(vehicle): params = parameters.get() filename = 'config/{}-{}-{}.txt'.format(params['prefix'], vehicle[0], params['suffix']) try: files = os.listdir('config') if filename in files: return (filename) else: return (False) except: return (False)
def configure(console): params = parameters.get() temp_ip = params['temp'] while True: prompt = read.serial(console) if prompt: if prompt[0][-1:] == "#": write.serial(console, "conf t") prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') prompt = read.serial(console) time.sleep(1) write.serial(console, "int bvi1") prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') prompt = read.serial(console) time.sleep(1) write.serial(console, "ip address {} 255.255.255.0".format(temp_ip)) prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') prompt = read.serial(console) time.sleep(1) write.serial(console, "exit") prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') prompt = read.serial(console) time.sleep(1) write.serial(console, "exit") prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') prompt = read.serial(console) time.sleep(1) print("Interface configured") return (True) else: print("Not enabled.") return (False) else: write.serial(console, "\r")
def check(): params = parameters.get() template = params['template'] if not os.path.isdir("config"): print("No config directory found, creating directry...") try: os.mkdir('config') except OSError: print("Failed to create config directory") else: print("Created config directory") while True: if not os.path.exists('{}'.format(template)): print( "{} not found, copy template file there or change config to suit" .format(template)) input("Press ENTER to continue...") else: return (params)
def upgrade(console): params = parameters.get() ip = params['ip'] while True: prompt = read.serial(console) if prompt: if prompt[0][-1:] == "#": write.serial( console, "archive download-sw /overwrite /force-reload tftp://{}/ap3g2-k9w7-tar.153-3.JI1.tar" .format(ip)) prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') print("Transferring image and upgrading...") prompt = read.serial(console) time.sleep(1) return (True) else: print("Not enabled.") return (False) else: write.serial(console, "\r")
def check(console): ''' Checks configuration status ''' print('Checking configuration') params = parameters.get() while True: prompt = read.serial(console) if prompt: if len(prompt[0]) == 3: if prompt[0] == 'ap>' or prompt[0] == 'ap#': print('Not configured') return (False) elif prompt[0][:2] == 'AP': if prompt[0][-1:] == '>' or prompt[0][-1:] == '#': print('Not configured') return (False) elif len(prompt[0]) >= 9: if prompt[0][-1] == '#' or prompt[0][-1] == '>': if prompt[0].startswith( params['prefix']) and prompt[0].endswith( params['suffix']): print("Already configured as {}".format(prompt[0])) return ('something') else: print('Unable to detect current configuration.') return ('something') elif any("--More--" in s for s in prompt): write.serial(console, ' ') else: write.serial(console, "\r") time.sleep(1)
def main(): if not portInUse(69): launchTFTP() already_configured = False while True: # Get parameters params = parameters.get() # Get vehicle details vehicle = details.get() # Check config exists if not config.exists(vehicle): config.generate(vehicle) # Initialize serial console = initialize.port() # Check if finished booting, otherwise waits until done booted = boot.check(console) # Check autonomous status is_autonomous = autonomous.check(console) # Check upgrade status upgraded = version.check(console) # Check config configured = configuration.check(console) # Set interface as set if already configured if configured: interface_set = True already_configured = True elif not configured: interface_set = False # Login / convert to autonomous if not is_autonomous: logged_in = login.now(console) if logged_in: autonomous.convert(console) # Wait for boot booted = boot.check(console) if is_autonomous: enabled = enable.check(console) if not configured: interface_set = interface.check(console) if not interface_set: enabled = enable.check(console) interface_configured = interface.configure(console) interface_set = True if interface_set: if not upgraded: enabled = enable.check(console) upgraded = version.upgrade(console) booted = boot.check(console) if upgraded: if not configured: # Check if enabled enabled = enable.check(console) # Copy config, prompt if already exists configuration.copy(console, vehicle) # Loads config into running-config configuration.load(console, vehicle) # Checks if configured configured = configuration.check(console) if already_configured: overwrite = configuration.overWrite() if overwrite: interface_configured = interface.check(console) if not interface_configured: interface.configure(console) enabled = enable.check(console) configuration.copy(console, vehicle) configured = configuration.load(console, vehicle) test.command(console) finalize.finish(console, vehicle)
def finish(console, vehicle): params = parameters.get() hostname = '{}{}{}'.format(params['prefix'], vehicle[0], params['suffix']) if params['case'] == 'lower': hostname = hostname.lower() elif params['case'] == 'upper': hostname = hostname.upper() while True: prompt = read.serial(console) if prompt: if prompt[0][-1:] == "#": write.serial(console, "wr mem") prompt = read.serial(console) time.sleep(1) write.serial(console, '\r') prompt = read.serial(console) time.sleep(1) print("Config written to memory") write.serial(console, '\r') time.sleep(2) prompt = read.serial(console) configured_name = prompt[0][:-1] close.serial(console) break else: write.serial(console, "\r") if configured_name != hostname: print("!!WARNING!!") print('Configured hostname DOES NOT match specified vehicle [FAIL]') print("Configured: {}".format(configured_name)) print("Specified: {}".format(vehicle[0])) while True: try_again = input("Try again (y) or ignore (n): ") if try_again == 'n': break elif try_again == "y": break else: print("How hard is it to press y or n") if configured_name == hostname or try_again == 'n': print("--------------------------------------") print("CONGRATULATIONS! MADE IT TO THE END!") print("Check config via putty.") print("Don't forget to update records") print("--------------------------------------") print('--------------------------------------') print(' VEHICLE: {}'.format(vehicle[0])) print(' IP: {}'.format(vehicle[1])) print(' HOSTNAME: {}'.format(hostname)) print('--------------------------------------') print('') print('Milkshakes are on {}'.format(random.choice(names))) input('Ready for another round? [ENTER]')