def enter_mode(self, mode=cli.CLIMode.CONFIG, interface=None): """ Enter mode based on mode string ('normal', 'enable', or 'configure'). :param mode: The CLI mode to enter. It must be 'normal', 'enable', or 'configure' :param interface: If entering sub-if mode, interface to enter :raises UnknownCLIMode: if mode is not "normal", "enable", or "configure" """ if mode == cli.CLIMode.NORMAL: self.enter_mode_normal() elif mode == cli.CLIMode.ENABLE: self.enter_mode_enable() elif mode == cli.CLIMode.CONFIG: self.enter_mode_config() elif mode == cli.CLIMode.SUBIF: self.enter_mode_subif(interface) else: raise exceptions.UnknownCLIMode(mode=mode)
def current_cli_mode(self): """ Determine the current mode of the CLI. Sends a newline and checks which prompt pattern matches. :return: current CLI mode. :raises UnknownCLIMode: if the current mode could not be detected. """ (output, match) = self._send_line_and_wait('', [ self.CLI_SHELL_PROMPT, self.CLI_NORMAL_PROMPT, self.CLI_ENABLE_PROMPT, self.CLI_CONF_PROMPT ]) modes = { self.CLI_SHELL_PROMPT: cli.CLIMode.SHELL, self.CLI_NORMAL_PROMPT: cli.CLIMode.NORMAL, self.CLI_ENABLE_PROMPT: cli.CLIMode.ENABLE, self.CLI_CONF_PROMPT: cli.CLIMode.CONFIG } if match.re.pattern not in modes: raise exceptions.UnknownCLIMode(prompt=output) return modes[match.re.pattern]
def enter_mode_config(self): """ Puts the CLI into config mode, if it is not there already. :raises UnknownCLIMode: if mode is not "normal", "enable", or "configure" """ self._log.debug('Going to Config mode') mode = self.current_cli_mode() if mode == cli.CLIMode.NORMAL: self._enable() self._send_line_and_wait('config terminal', self.CLI_CONFIG_PROMPT) elif mode == cli.CLIMode.ENABLE: self._send_line_and_wait('config terminal', self.CLI_CONFIG_PROMPT) elif mode == cli.CLIMode.CONFIG: self._log.debug('Already at Config, doing nothing') elif mode == cli.CLIMode.SUBIF: self._send_line_and_wait('exit', self.CLI_CONFIG_PROMPT) else: raise exceptions.UnknownCLIMode(mode=mode)
def enter_mode_enable(self): """ Puts the CLI into enable mode. Note this will go 'backwards' if needed (e.g., exiting config mode) :raises UnknownCLIMode: if mode is not "normal", "enable", or "configure" """ self._log.debug('Going to Enable mode') mode = self.current_cli_mode() if mode == cli.CLIMode.NORMAL: self._enable() elif mode == cli.CLIMode.ENABLE: self._log.debug('Already at Enable, doing nothing') elif mode == cli.CLIMode.CONFIG: self._send_line_and_wait('end', self.CLI_ENABLE_PROMPT) elif mode == cli.CLIMode.SUBIF: self._send_line_and_wait('end', self.CLI_ENABLE_PROMPT) else: raise exceptions.UnknownCLIMode(mode=mode)
def enter_mode_config(self): """ Puts the CLI into config mode, if it is not there already. In this mode, you can make changes in the configuration. :raises UnknownCLIMode: if mode is not "normal", "configure" """ self._log.debug('Going to Config mode') mode = self.current_cli_mode() if mode == cli.CLIMode.NORMAL: self._send_line_and_wait('configure', self.CLI_CONFIG_PROMPT) elif mode == cli.CLIMode.CONFIG: self._log.debug('Already at Config, doing nothing') else: raise exceptions.UnknownCLIMode(mode=mode)
def enter_mode(self, mode=cli.CLIMode.CONFIG, force=False): """ Enter the mode based on mode string ('normal','config'). :param mode: The CLI mode to enter. It must be 'normal', 'enable', or 'configure' :type mode: string :param force: Discard commits and force enter requested mode :type force: Boolean :raises UnknownCLIMode: if mode is not "normal", "configure" """ if mode == cli.CLIMode.NORMAL: self.enter_mode_normal(force) elif mode == cli.CLIMode.CONFIG: self.enter_mode_config() else: raise exceptions.UnknownCLIMode(mode=mode)
def enter_mode(self, mode=cli.CLIMode.ENABLE, reinit=True): """ Enter mode based on name ('normal', 'enable', 'configure', or 'shell'). :param mode: The CLI mode to enter. It must be 'normal', 'enable', or 'configure'. Use :class:`CLIMode` values. :param reinit: bool should this function attempt to repair connection. :raises UnknownCLIMode: if mode is not "normal", "enable", "configure", or "shell". :raises CLINotRunning: if the CLI is not running. """ try: if mode == cli.CLIMode.NORMAL: self.enter_mode_normal() elif mode == cli.CLIMode.ENABLE: self.enter_mode_enable() elif mode == cli.CLIMode.CONFIG: self.enter_mode_config() elif mode == cli.CLIMode.SHELL: self.enter_mode_shell() else: raise exceptions.UnknownCLIMode(mode=mode) except (socket.error, exceptions.ConnectionError) as e: if reinit: self._log.debug('Socket or connection error raised in ' 'RVBD_CLI.enter_mode():' ' {error}'.format(error=e)) self._log.debug('RVBD_CLI.enter_mode() - Attempting to ' 'reinitialize connection') self._cleanup_helper() self.start() # assuming all is well now. Recursively calling enter_mode() self.enter_mode(mode=mode, reinit=False) else: raise e
def enter_mode_normal(self, force=False): """ Puts the CLI into the 'normal' mode. In this mode you can run commands, but you cannot change the configuration. :param force: Will force enter 'normal' mode, discarding all changes that haven't been committed. :type force: Boolean :raises CLIError: if unable to go from "configure" mode to "normal" This happens if "commit" is not applied after config changes :raises UnknownCLIMode: if mode is not "normal" or "configure" """ self._log.info('Going to normal mode') mode = self.current_cli_mode() if mode == cli.CLIMode.NORMAL: self._log.debug('Already at normal, doing nothing') elif mode == cli.CLIMode.CONFIG: if force: self._log.debug('Entering normal mode, discarding all commits') self._send_line_and_wait( 'exit discard', self.CLI_NORMAL_PROMPT) else: self._log.debug('Entering normal mode') (output, match_res) = self._send_line_and_wait( 'exit', [self.CLI_ERROR_PROMPT, self.CLI_NORMAL_PROMPT]) if re.match(self.CLI_ERROR_PROMPT, match_res.string): raise exceptions.CLIError( command="exit", output=match_res.string, mode=mode) else: raise exceptions.UnknownCLIMode(mode=mode)