def run(self, hostname, source, destination, direction, username='', password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username='', enable_password='') scp = Secure_Copy.Secure_Copy(hostname, self._username, self._password) # TODO: This should be done when keys are re-generated scp.erase_existing_ssh_key_for_host() if direction == 'to': success = scp.send_file(source, destination) if direction == 'from': success = scp.get_file(source, destination) if success: return (True, "File Copied!") else: return (False, "Failed")
def run(self, via, device, command, conf_mode=False, username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) devices = device.split(',') device_output = [] has_failures = False for d in devices: session = ztp_utils.start_session(d, self._username, self._password, self._enable_username, self._enable_password, via) (success, output) = ztp_utils.send_commands_to_session( session, command, conf_mode) if success: device_output.append({"device": d, "output": output}) else: device_output.append({"device": d, "output": "Failed"}) has_failures = True if len(devices) == 1 and has_failures: return (False, "Failed") return (True, device_output)
def run(self, device, username='', password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username='', enable_password='') config_dir = '%s/%s' % (self._config_archive_dir, device) try: # Create directory for device if it doesn't exist if not os.path.exists(config_dir): os.makedirs(config_dir) except IOError: sys.stderr.write("Could not create directory for '%s'\r\n" % device) return (False, "Failed") scp = Secure_Copy.Secure_Copy(device, self._username, self._password) timestamp = int(time.time()) filename = '%s/%s_%s.cfg' % (config_dir, device, timestamp) if scp.get_file('RunConfig', filename): return (True, "Success") return (False, "Failed")
def run(self, via, device, flash, tftp_server='', filename='', username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) if tftp_server: self._tftpserver = tftp_server if filename: self._filename = filename session = ztp_utils.start_session(device, self._username, self._password, self._enable_username, self._enable_password, via) if session.login(): if session.enter_enable_mode(): if session.upgrade_code_by_tftp(self._tftpserver, self._filename, flash): session.logout() return (True, "Success") return (False, "Failed")
def run(self, via, template, template_dir='', variables='{}', conf_mode=False, username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) if template_dir: self._template_dir = template_dir if variables: try: variables = json.loads(variables) except ValueError: sys.stderr.write("variables is not in JSON format!\r\n") return (False, "Failed") # Process Template (success, command) = ztp_utils.process_template(template, self._template_dir, variables) return (True, command) device_output = [] has_failures = False if (self._ruckus_controller_ip != None): session = ztp_utils.ruckus_controller_start_session( self._ruckus_controller_ip, self._ruckus_controller_username, self._ruckus_controller_password, self._ruckus_controller_enable_username, self._ruckus_controller_enable_password, via) (success, output) = ztp_utils.send_commands_to_session( session, command, conf_mode) if success: device_output.append({ "device": self._ruckus_controller_ip, "command": command, "output": output }) else: device_output.append({ "device": self._ruckus_controller_ip, "command": command, "output": "Failed" }) has_failures = True session.logout() if len(self._ruckus_controller_ip) != None and has_failures: return (False, "Failed") return (True, device_output)
def run(self, via, device, username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) session = ztp_utils.start_session(device, self._username, self._password, self._enable_username, self._enable_password, via) command = 'show version' (success, results) = ztp_utils.send_commands_to_session(session, command, conf_mode=False) if success: # Built JSON Record from parsed results result = {} unit_and_version = re.compile( '(^\s+UNIT )(\d+)(:.+)(labeled as )(.+)') boot = re.compile('(^\s+.+Boot-Monitor.+ Version:)([\d\w\.]+)') hardware = re.compile('(^\s+HW: )(Stackable )?([\w\d\-]+)') for line in results[0]['output']: match = unit_and_version.match(line) if match: if 'firmware' not in results: result['firmware'] = [] unit = match.group(2) version = match.group(5) result['firmware'].append({ 'unit': int(unit), 'version': version.upper() }) match = hardware.match(line) if match: print("matched hardware") hardware_type = match.group(3) result.update({'hardware': hardware_type.upper()}) match = boot.match(line) if match: boot_version = match.group(2) result.update({'boot': boot_version.upper()}) return (True, json.dumps(result)) return (False, "Failed")
def run(self, via, device, template, template_dir='', variables='{}', conf_mode=False, username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) if template_dir: self._template_dir = template_dir if variables: try: variables = json.loads(variables) except ValueError: sys.stderr.write("variables is not in JSON format!\r\n") return (False, "Failed") # Process Template (success, command) = ztp_utils.process_template(template, self._template_dir, variables) device_output = [] """ For Simulation Purposes """ return (True, command) devices = device.split(',') has_failures = False for d in devices: session = ztp_utils.start_session(d, self._username, self._password, self._enable_username, self._enable_password, via) (success, output) = ztp_utils.send_commands_to_session( session, command, conf_mode) if success: device_output.append({"device": d, "output": output}) else: device_output.append({"device": d, "output": "Failed"}) has_failures = True session.logout() if len(devices) == 1 and has_failures: return (True, "Failed") return (True, device_output)
def run(self, via, device, excel_key, additional_variables='{}', username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) (success, config) = ztp_utils.create_configuration(excel_key, self._excel_file, self._template_dir, additional_variables) if success: session = ztp_utils.start_session(device, username, password, enable_username, enable_password, via) if session.login(): if session.enter_enable_mode(): try: cfg_file = open(self._filename, 'w') cfg_file.write(config) cfg_file.close() except IOError: sys.stderr.write( "Could not write configuration to temp file\r\n") session.logout() return (False, "Failed") scp = Secure_Copy.Secure_Copy(device, self._username, self._password) # TODO: This should be done when generate_keys is done scp.erase_existing_ssh_key_for_host() if scp.send_file(self._filename, 'StartConfig'): session.reload(writemem=False) os.remove(self._filename) return (True, "Success") os.remove(self._filename) sys.stderr.write( "Could not generate configuration file from excel\r\n") return (False, "Failed")
def run(self, via, device, username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) session = ztp_utils.start_session(device, self._username, self._password, self._enable_username, self._enable_password, via) command = 'show module | include ^U' (success, results) = ztp_utils.send_commands_to_session(session, command, conf_mode=False) if success: # Built JSON Record from parsed results modules = {} last_unit = '' for line in results[0]['output']: unit = line[0:7].split(':')[0][1:] module = line[0:7].split(':')[1][1:].rstrip() module_name = line[7:50].rstrip() ports = line[50:-1].split()[1] if unit != last_unit: modules[unit] = [] modules[unit].append({ "module": module, "name": module_name, "ports": ports }) last_unit = unit return (True, json.dumps(modules)) return (False, "Failed")
def run(self, via, device, username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) session = ztp_utils.start_session(device, self._username, self._password, self._enable_username, self._enable_password, via) command = 'show flash' (success, results) = ztp_utils.send_commands_to_session(session, command, conf_mode=False) if success: # Built JSON Record from parsed results flash = {} unit = re.compile('^Stack unit [0-9]+') primary = re.compile('(.)+Pri(.)+Version(.)+') secondary = re.compile('(.)+Sec(.)+Version(.)+') boot = re.compile('(.)+Boot-Monitor(.)+Version(.)+') for line in results[0]['output']: match = unit.match(line) if match: unit_number = match.group().split()[-1] flash[unit_number] = {} match = primary.match(line) if match: version = match.group().split(':')[-1].split()[0] flash[unit_number].update({'primary': version}) match = secondary.match(line) if match: version = match.group().split(':')[-1].split()[0] flash[unit_number].update({'secondary': version}) match = boot.match(line) if match: version = match.group().split(':')[-1].split()[0] flash[unit_number].update({'boot': version}) return (True, json.dumps(flash)) return (False, "Broken")
def run(self, via, device, keytype='rsa', modulus='2048', username='', password='', enable_username='', enable_password=''): ztp_utils.replace_default_userpass(self, username, password, enable_username, enable_password) session = ztp_utils.start_session(device, username, password, enable_username, enable_password, via) if session.login(): if session.enter_enable_mode(): if session.enter_configuration_mode(): session.create_crypto_keys(keytype, modulus) session.exit_configuration_mode() session.logout() return (True, "Success") return (False, "Failure")