def login(self, login_type='ssh'): """ Login to linux/unix shell (bash terminal assumed) :param login_type: SSH or Telnet :return: pexpect spawn object Authentication types: - username and password - certificate based """ validate_login_type(login_type) login_cmd = self.ssh_driver if login_type.lower( ) == 'ssh' else self.telnet_driver self.child = pexpect.spawn(login_cmd) i = self.child.expect(PEXPECT_ERRORS + [UNIX_PROMPT, PASSWORD_PROMPT]) if i == 0 or i == 1: logging.debug(device_connection_error_msg(self.device)) clean_up_error(self.child, i) elif i == 2: logging.debug(bash_success_msg(self.device)) return self.child elif i == 3: self.child.sendline(self.password) j = self.child.expect(PEXPECT_ERRORS + [UNIX_PROMPT]) if j == 0 or j == 1: logging.debug(user_password_error_msg(self.device)) clean_up_error(self.child, j) elif j == 2: logging.debug(bash_success_msg(self.device))
def config_db_backup(self, filename='', path='/home/basic', prompt=VIPTELA_PRIV_PROMPT, timeout=60): """ Backup vManage configuration database. Only valid for vManage devices. :param filename: Name of backup file (excluding .tar.gz) :param path: Path to save to file :param prompt: Expected Prompt :param timeout: Timeout in seconds to wait for command to complete :return: True if successful """ if not filename: time_now = strftime('%Y-%m-%d-%H%M%S') filename = '{0}-backup-{1}'.format(self.device, time_now) backup_command = 'request nms configuration-db backup path {0}/{1}'.format( path, filename) self.child.sendline(backup_command) i = self.child.expect(PEXPECT_ERRORS + [prompt], timeout=timeout) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, backup_command)) clean_up_error(self.child, i) elif i == 2: logging.debug('{0} backup command completed'.format(self.device)) return True
def enable_api(self, port=443): """ Enable ASA API. Must have rest API extensions installed on flash :param port: API port number :return: True if successful """ self.configuration_mode() http_enable_cmd = 'http server enable {0}'.format(port) self.child.sendline(http_enable_cmd) i = self.child.expect(PEXPECT_ERRORS + [CISCO_CONFIG_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, http_enable_cmd)) clean_up_error(self.child, i) elif i == 2: rest_api_enable_cmd = 'rest-api agent' self.child.sendline(rest_api_enable_cmd) j = self.child.expect(PEXPECT_ERRORS + [CISCO_CONFIG_PROMPT]) if j == 0 or j == 1: logging.debug( send_command_error_msg(self.device, rest_api_enable_cmd)) clean_up_error(self.child, j) elif j == 2: if 'error' not in j.child.before: logging.debug(rest_api_enabled_success_msg(self.device)) return True else: return False
def login(self): """ Login to Viptela devices Authentication types: - username and password - certificate based """ login_cmd = self.ssh_driver self.child = pexpect.spawn(login_cmd, timeout=self.timeout) i = self.child.expect(PEXPECT_ERRORS + [PASSWORD_PROMPT, VIPTELA_PRIV_PROMPT]) if i == 0 or i == 1: logging.debug(device_connection_error_msg(self.device)) clean_up_error(self.child, i, get_error=False) raise LoginTimeoutError(device_connection_error_msg(self.device)) elif i == 2: self.child.sendline(self.password) j = self.child.expect(PEXPECT_ERRORS + [VIPTELA_PRIV_PROMPT]) if j == 0 or j == 1: logging.debug(user_password_error_msg(self.device)) clean_up_error(self.child, j, get_error=False) raise LoginCredentialsError( user_password_error_msg(self.device)) elif j == 2: logging.debug(privilege_exec_success_msg(self.device)) elif i == 3: logging.debug(privilege_exec_success_msg(self.device))
def enable_mode(child, device, enable_password='', command='enable'): """ Enter enable mode on device :param child: Pexpect spawn child process :param device: Device name :param enable_password: Enable password if required :param command: Command to enter enable mode """ child.sendline(command) i = child.expect(PEXPECT_ERRORS + [PASSWORD_PROMPT, CISCO_PRIV_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(device, command)) clean_up_error(child, i) elif i == 2: if not enable_password: child.close() raise EnablePasswordError(enable_password_required_msg(device)) child.sendline(enable_password) j = child.expect(PEXPECT_ERRORS + [CISCO_PRIV_PROMPT]) if j == 0 or j == 1: logging.debug(enable_password_error_msg(device)) clean_up_error(child, j, get_error=False) raise EnablePasswordError(enable_password_error_msg(device)) elif j == 2: logging.debug(privilege_exec_success_msg(device)) elif i == 3: logging.debug(privilege_exec_success_msg(device))
def login(self, login_type='ssh'): """ Login to Juniper Junos :param login_type: SSH or Telnet :return: pexpect spawn object Authentication types: - username and password - certificate based """ validate_login_type(login_type) login_cmd = self.ssh_driver if login_type.lower( ) == 'ssh' else self.telnet_driver self.child = pexpect.spawn(login_cmd, timeout=self.timeout) i = self.child.expect(PEXPECT_ERRORS + [ PASSWORD_PROMPT, JUNIPER_ALT_SHELL_PROMPT, JUNIPER_SHELL_PROMPT, JUNIPER_OPER_PROMPT ]) if i == 0 or i == 1: logging.debug(device_connection_error_msg(self.device)) clean_up_error(self.child, i, get_error=False) raise LoginTimeoutError(device_connection_error_msg(self.device)) elif i == 2: self.child.sendline(self.password) j = self.child.expect(PEXPECT_ERRORS + [ JUNIPER_ALT_SHELL_PROMPT, JUNIPER_SHELL_PROMPT, JUNIPER_OPER_PROMPT ]) if j == 0 or j == 1: logging.debug(user_password_error_msg(self.device)) clean_up_error(self.child, j, get_error=False) raise LoginCredentialsError( user_password_error_msg(self.device)) elif j == 2 or j == 3: logging.debug(juniper_shell_mode_success_msg(self.device)) self.operational_mode(child=self.child, device=self.device) elif j == 4: logging.debug(operational_mode_success_msg(self.device)) elif i == 3 or i == 4: logging.debug(juniper_shell_mode_success_msg(self.device)) self.operational_mode(child=self.child, device=self.device) elif i == 5: logging.debug(operational_mode_success_msg(self.device))
def configuration_mode(self, command='configure terminal'): """ Enter configuration mode :param command: Command to enter configuration mode :return: True if successful """ self.child.sendline(command) i = self.child.expect(PEXPECT_ERRORS + [VIPTELA_CONFIG_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(configuration_mode_success_msg(self.device)) return True
def save_config(self, command='commit'): """ Save device config :return: True if successful """ self.child.sendline(command) i = self.child.expect(PEXPECT_ERRORS + [JUNIPER_CONFIG_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(save_config_success_msg(self.device)) return True
def enable_scp(self, command='ssh scopy enable'): """ Enable SCP to facilitate secure file transfer to device :return: True if successful """ self.configuration_mode() self.child.sendline(command) i = self.child.expect(PEXPECT_ERRORS + [CISCO_CONFIG_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(scp_enabled_success_msg(self.device)) return True
def operational_mode(child, device, command='cli'): """ Move into operational mode if in root cli :param child: pexpect spawn child :param device: name of device :param command: Command to enter operational mode :return: pexpect.spawn child """ child.sendline(command) i = child.expect(PEXPECT_ERRORS + [JUNIPER_OPER_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(device, command)) clean_up_error(child, i) elif i == 2: logging.debug(operational_mode_success_msg(device))
def login(self, login_type='ssh'): """ Login to Cisco IOS, IOS-XE, NXOS :param login_type: SSH or Telnet Authentication types: - username and password - certificate based """ validate_login_type(login_type) login_cmd = self.ssh_driver if login_type.lower( ) == 'ssh' else self.telnet_driver self.child = pexpect.spawn(login_cmd, timeout=self.timeout) i = self.child.expect( PEXPECT_ERRORS + [PASSWORD_PROMPT, CISCO_USER_PROMPT, CISCO_PRIV_PROMPT]) if i == 0 or i == 1: logging.debug(device_connection_error_msg(self.device)) clean_up_error(self.child, i, get_error=False) raise LoginTimeoutError(device_connection_error_msg(self.device)) elif i == 2: self.child.sendline(self.password) j = self.child.expect(PEXPECT_ERRORS + [CISCO_USER_PROMPT, CISCO_PRIV_PROMPT]) if j == 0 or j == 1: logging.debug(user_password_error_msg(self.device)) clean_up_error(self.child, j, get_error=False) raise LoginCredentialsError( user_password_error_msg(self.device)) elif j == 2: logging.debug(user_exec_success_msg(self.device)) self.enable_mode(self.child, self.device, self.enable_password) elif j == 3: logging.debug(privilege_exec_success_msg(self.device)) elif i == 3: self.enable_mode(self.child, self.device, self.enable_password) elif i == 4: logging.debug(privilege_exec_success_msg(self.device))
def enable_api(self, command='set system services netconf ssh'): """ Enable device API :return: True if successful """ if self.get_prompt().endswith('>'): self.configuration_mode() self.child.sendline(command) i = self.child.expect(PEXPECT_ERRORS + [JUNIPER_CONFIG_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(netconf_enabled_success_msg(self.device)) return self.save_config()
def disable_paging(self, prompt='', command='set cli screen-length 0'): """ Disable paging of long terminal outputs. Represented as <more> Paging is disabled from operational mode (>). :param prompt: Prompt to expect :param command: Command to disable paging :return: True if successful """ if not prompt: prompt = self.get_prompt() self.child.sendline(command) i = self.child.expect(PEXPECT_ERRORS + [prompt]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(disable_paging_success_msg(self.device)) return True
def save_config(self, source='running-config', destination='startup-config'): """ Save device config :param source: Source file name :param destination: Destination file name :return: True if successful """ command = 'copy {0} {1}'.format(source, destination) if self.get_prompt().endswith(')#'): self.child.sendline('end') self.child.sendline(command) # ASA has a timing issue when saving config. Adding # in 1 second of sleep before expecting prompt to compensate time.sleep(1) i = self.child.expect(PEXPECT_ERRORS + ['.*filename.*', CISCO_PRIV_PROMPT]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: self.child.sendcontrol('m') j = self.child.expect(PEXPECT_ERRORS + [CISCO_PRIV_PROMPT]) if j == 0 or j == 1: logging.debug(save_config_error_msg(self.device)) clean_up_error(self.child, j) elif i == 2: logging.debug(save_config_success_msg(self.device)) return True elif i == 3: logging.debug(save_config_success_msg(self.device)) return True
def disable_paging(self, prompt=VIPTELA_PRIV_PROMPT, command='paginate false'): """ Disable paging of long terminal outputs. Represented as <more> :param command: Command to disable pagination :param prompt: Prompt to expect :return: True if successful """ if not prompt: prompt = self.get_prompt() self.child.sendline(command) i = self.child.expect(PEXPECT_ERRORS + [prompt]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(disable_paging_success_msg(self.device)) return True
def disable_paging(self, prompt='', command='terminal pager 0'): """ Disable paging of long terminal outputs. Represented as <more> :param prompt: Prompt to expect :param command: Command to disable paging :return: True if successful """ if not prompt: prompt = self.get_prompt() self.child.sendline(command) # ASA has a timing issue when saving config. Adding # in 1 second of sleep before expecting prompt to compensate time.sleep(1) i = self.child.expect(PEXPECT_ERRORS + [prompt]) if i == 0 or i == 1: logging.debug(send_command_error_msg(self.device, command)) clean_up_error(self.child, i) elif i == 2: logging.debug(disable_paging_success_msg(self.device)) return True