def __init__(self): CommandMode.__init__(self, self.PROMPT, self.ENTER_COMMAND, self.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=self.exit_action_map(), enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
def __init__(self, context): """ Initialize Default command mode, only for cases when session started not in enable mode :param context: """ self._context = context CommandMode.__init__(self, DefaultCommandMode.PROMPT, DefaultCommandMode.ENTER_COMMAND, DefaultCommandMode.EXIT_COMMAND)
def __init__(self, context): """Initialize Enable command mode - default command mode for Cisco Shells.""" self._context = context CommandMode.__init__( self, EnableCommandMode.PROMPT, EnableCommandMode.ENTER_COMMAND, EnableCommandMode.EXIT_COMMAND, )
def __init__(self, resource_config, api): self.resource_config = resource_config self._api = api CommandMode.__init__(self, ConfigCommandMode.PROMPT, ConfigCommandMode.ENTER_COMMAND, ConfigCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=self.exit_action_map(), enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
def __init__(self, host, username, password): self.cli = CLI() self.mode = CommandMode(r'#')# for example r'%\s*$' self.clish_mode = CommandMode(r'>', enter_command='clish', exit_command='exit')# for example r'%\s*$' self.mode.add_child_node(self.clish_mode) self.session_types = [SSHSession(host=host, username=username, password=password)] self.session = self.cli.get_session(command_mode=self.mode, new_sessions=self.session_types)
def __init__(self, context): """Initialize Config command mode.""" exit_action_map = { self.PROMPT: lambda session, logger: session.send_line("exit", logger) } CommandMode.__init__( self, ConfigCommandMode.PROMPT, ConfigCommandMode.ENTER_COMMAND, ConfigCommandMode.EXIT_COMMAND, exit_action_map=exit_action_map, )
def setUp(self): self._prompt = Mock() self._enter_command = Mock() self._enter_action_map = Mock() self._enter_error_map = Mock() self._exit_command = Mock() self._exit_action_map = Mock() self._exit_error_map = Mock() self._enter_actions = Mock() self._session = Mock() self._command_mode = CommandMode(self._prompt, enter_command=self._enter_command, enter_action_map=self._enter_action_map, enter_error_map=self._enter_error_map, exit_command=self._exit_command, exit_action_map=self._exit_action_map, exit_error_map=self._exit_error_map, enter_actions=self._enter_actions) self._logger = Mock()
def __init__(self): """ Initialize Default command mode, only for cases when session started not in enable mode :param context: """ CommandMode.__init__(self, DefaultCommandMode.PROMPT, DefaultCommandMode.ENTER_COMMAND, DefaultCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=self.exit_action_map(), enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
def __init__(self, resource_config, api): """ Initialize Config command mode :param resource_config: """ self.resource_config = resource_config self._api = api self._root_password = None CommandMode.__init__(self, RootCommandMode.PROMPT, RootCommandMode.ENTER_COMMAND, RootCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map())
def send_any_cmd(self, context, sendcmd): """ :param InitCommandContext context : passed in by cloudshell :param str sendcmd: the command to send to the CLI """ cli = CLI() mode = CommandMode(r'#') # for example r'%\s*$' session = CloudShellAPISession( host=context.connectivity.server_address, token_id=context.connectivity.admin_auth_token, domain=context.reservation.domain) address = context.resource.address user = context.resource.attributes['LinuxServerShell.User'] password = session.DecryptPassword( context.resource.attributes['LinuxServerShell.Password']).Value session_types = [ SSHSession(host=address, username=user, password=password) ] with cli.get_session(session_types, mode) as default_session: out = default_session.send_command(sendcmd) print(out) return out
def __init__(self, resource_config, api): """ Initialize Config command mode :param resource_config: """ self.resource_config = resource_config self._api = api CommandMode.__init__( self, AristaConfigCommandMode.PROMPT, AristaConfigCommandMode.ENTER_COMMAND, AristaConfigCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), )
def __init__(self, resource_config, api): """ Initialize Config command mode :param resource_config: """ exit_action_map = { self.PROMPT: lambda session, logger: session.send_line('exit', logger) } CommandMode.__init__(self, CiscoIOSXRAdminCommandMode.PROMPT, CiscoIOSXRAdminCommandMode.ENTER_COMMAND, CiscoIOSXRAdminCommandMode.EXIT_COMMAND, exit_action_map=exit_action_map)
def __init__(self, resource_config, api): """ Initialize Default command mode, only for cases when session started not in enable mode :param resource_config: """ self.resource_config = resource_config self._api = api CommandMode.__init__( self, AristaDefaultCommandMode.PROMPT, AristaDefaultCommandMode.ENTER_COMMAND, AristaDefaultCommandMode.EXIT_COMMAND, )
def __init__(self): """ Initialize Config command mode :param context: """ exit_action_map = {self.PROMPT: lambda session, logger: session.send_line("exit", logger)} CommandMode.__init__(self, EnableCommandMode.PROMPT, EnableCommandMode.ENTER_COMMAND, EnableCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=exit_action_map, enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
def __init__(self, resource_config, api): """ Initialize Enable command mode - default command mode for Arista Shells :param resource_config: """ self.resource_config = resource_config self._api = api self._enable_password = None CommandMode.__init__( self, AristaEnableCommandMode.PROMPT, AristaEnableCommandMode.ENTER_COMMAND, AristaEnableCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), )
def __init__(self, resource_config, api): """ Initialize Default command mode, only for cases when session started not in enable mode :param resource_config: """ self.resource_config = resource_config self._api = api CommandMode.__init__(self, DefaultCommandMode.PROMPT, DefaultCommandMode.ENTER_COMMAND, DefaultCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=self.exit_action_map(), enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
def __init__(self, resource_config, api): """ Initialize Enable command mode - default command mode for Shells :param context: """ self.resource_config = resource_config self._api = api CommandMode.__init__(self, EnableCommandMode.PROMPT, EnableCommandMode.ENTER_COMMAND, EnableCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=self.exit_action_map(), enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
class CreateSession(): def __init__(self, host, username, password, logger=None): self.cli = CLI() self.logger = logger self.mode = CommandMode(r'$') # for example r'%\s*$' enable_action_map = { "[Pp]assword for {}".format(username): lambda session, logger: session.send_line(password, logger) } self.elevated_mode = CommandMode(r'(?:(?!\)).)#\s*$', enter_command='sudo su', exit_command='exit', enter_action_map=enable_action_map) self.mode.add_child_node(self.elevated_mode) self.elevated_mode.add_parent_mode(self.mode) self.session_types = [ SSHSession(host=host, username=username, password=password) ] self.session = self.cli.get_session(command_mode=self.elevated_mode, new_sessions=self.session_types) def send_terminal_command(self, command, password=None): outp = [] out = None with self.session as my_session: if isinstance(command, list): for single_command in command: if password: single_command = '{command}'.format( command=single_command, password=password) # single_command = 'echo {password} | sudo -S sh -c "{command}"'.format(command=single_command, # password=password) self.logger.info( 'sending command {}'.format(single_command)) current_outp = my_session.send_command(single_command) outp.append(current_outp) self.logger.info('got output {}'.format(current_outp)) out = '\n'.join(outp) else: if password: command = '{command}'.format(command=command) out = my_session.send_command(command) return out
def __init__(self, resource_config, api): """Init command. :param resource_config: """ self.resource_config = resource_config self._api = api self._enable_password = None CommandMode.__init__( self, EnableCommandMode.PROMPT, EnableCommandMode.ENTER_COMMAND, EnableCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=self.exit_action_map(), enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map(), )
def __init__(self, host, username, password): self.cli = CLI() self.mode = CommandMode(r'#') # for example r'%\s*$' self.session_types = [ SSHSession(host=host, username=username, password=password) ] self.session = self.cli.get_session(command_mode=self.mode, new_sessions=self.session_types)
def __init__(self, host, username, password, logger=None): self.cli = CLI() self.logger = logger self.mode = CommandMode(r'$') # for example r'%\s*$' enable_action_map = { "[Pp]assword for {}".format(username): lambda session, logger: session.send_line(password, logger) } self.elevated_mode = CommandMode(r'(?:(?!\)).)#\s*$', enter_command='sudo su', exit_command='exit', enter_action_map=enable_action_map) self.mode.add_child_node(self.elevated_mode) self.elevated_mode.add_parent_mode(self.mode) self.session_types = [ SSHSession(host=host, username=username, password=password) ] self.session = self.cli.get_session(command_mode=self.elevated_mode, new_sessions=self.session_types)
def __init__(self, resource_config, api): """ Initialize Config command mode :param context: """ self.resource_config = resource_config self._api = api exit_action_map = { self.PROMPT: lambda session, logger: session.send_line('exit', logger)} CommandMode.__init__(self, ConfigCommandMode.PROMPT, ConfigCommandMode.ENTER_COMMAND, ConfigCommandMode.EXIT_COMMAND, enter_action_map=self.enter_action_map(), exit_action_map=exit_action_map, enter_error_map=self.enter_error_map(), exit_error_map=self.exit_error_map())
def __init__(self, context): self.context = context self.logger = LogHelper.get_logger(context) self.connection_type = get_attribute_by_name( context=self.context, attribute_name='CLI Connection Type') self.prompt_regex = get_attribute_by_name( context=self.context, attribute_name='CLI Prompt Regular Expression') self.mode = CommandMode(self.prompt_regex) self.session_types = None self._set_session_types()
class CreateSession(): def __init__(self, host, username, password): self.cli = CLI() self.mode = CommandMode(r'#')# for example r'%\s*$' self.clish_mode = CommandMode(r'>', enter_command='clish', exit_command='exit')# for example r'%\s*$' self.mode.add_child_node(self.clish_mode) self.session_types = [SSHSession(host=host, username=username, password=password)] self.session = self.cli.get_session(command_mode=self.mode, new_sessions=self.session_types) def send_terminal_command(self, command): with self.session as my_session: out = my_session.send_command(command) return out def send_clish_terminal_command(self, commands): outp = [] with self.cli.get_session(command_mode=self.mode, new_sessions=self.session_types) as session: with session.enter_mode(self.clish_mode) as clish_session: for command in commands: outp.append(clish_session.send_command(command)) return '\n'.join(outp) def config_license(self): outp = [] outp.append(self.send_terminal_command('\r\n')) outp.append(self.send_terminal_command('config log syslogd setting\r\n')) outp.append(self.send_terminal_command('set status enable\r\n')) outp.append(self.send_terminal_command('set server {}\r\n'.format(self.splunk_address))) outp.append(self.send_terminal_command('end\r\n')) return outp
def create_my_session(self): cli = CLI() mode = CommandMode(r'%\s*$#') # for example r'%\s*$' ip_address = '192.16.42.235' user_name = 'root' password = '******' session_types = [SSHSession(host=ip_address, username=user_name, password=password)] with cli.get_session(session_types, mode) as default_session: out = default_session.send_command('my command') print(out)
def __init__(self, host, username, password, logger=None): self.cli = CLI() self.logger = logger # the mode is the termination string - code will expect a"$" sign in this case to mark an end of input. self.mode = CommandMode(r'$') # enable_action_map = {"[Pp]assword for {}".format(username): lambda session, logger: session.send_line(password, logger)} # self.elevated_mode = CommandMode(r'(?:(?!\)).)#\s*$', enter_command='sudo su', exit_command='exit', enter_action_map=enable_action_map) # self.mode.add_child_node(self.elevated_mode) # self.elevated_mode.add_parent_mode(self.mode) self.session_types = [SSHSession(host=host, username=username, password=password)] # self.session = self.cli.get_session(command_mode=self.elevated_mode, new_sessions=self.session_types) self.session = self.cli.get_session(command_mode=self.mode, new_sessions=self.session_types)
def _get_device_session(self, context): """ :param context: :return: """ session = get_api(context) config = Linuxshell( name=context.resource.name).create_from_context(context) host = context.resource.address username = config.user password = session.DecryptPassword(config.password).Value cli = CLI() mode = CommandMode(prompt=r'.*\$') session_types = [ SSHSession(host=host, username=username, password=password) ] device_session = cli.get_session(session_types, mode) return device_session
def _cli_session_handler(self, context): self._cs_session_handler(context) logger = LogHelper.get_logger(context) self.cli_connection_type = context.resource.attributes['CLI Connection Type'] self.cli_prompt_regex = context.resource.attributes['CLI Prompt Regular Expression'] self.mode = CommandMode(self.cli_prompt_regex) self.session_types = None logger.info('CLI Connection Type: "%s"' % self.cli_connection_type) logger.info('CLI Prompt Regular Expression: "%s"' % self.cli_prompt_regex) if self.cli_connection_type == 'Auto': self.session_types = [SSHSession(host=self.address, username=self.user, password=self.cs_session.DecryptPassword(self.password_hash).Value), TelnetSession(host=self.address, username=self.user, password=self.cs_session.DecryptPassword(self.password_hash).Value)] elif self.cli_connection_type == 'Console': message = 'Unimplemented CLI Connection Type: "%s"' % self.cli_connection_type logger.error(message) raise GenericResourceDriver.UnImplementedCliConnectionType(message) elif self.cli_connection_type == 'SSH': self.session_types = [SSHSession(host=self.address, username=self.user, password=self.cs_session.DecryptPassword(self.password_hash).Value)] elif self.cli_connection_type == 'TCP': message = 'Unimplemented CLI Connection Type: "%s"' % self.cli_connection_type logger.error(message) raise GenericResourceDriver.UnImplementedCliConnectionType(message) elif self.cli_connection_type == 'Telnet': self.session_types = [TelnetSession(host=self.address, username=self.user, password=self.cs_session.DecryptPassword(self.password_hash).Value)] else: message = 'Unsupported CLI Connection Type: "%s"' % self.cli_connection_type logger.error(message) raise GenericResourceDriver.UnSupportedCliConnectionType(message)
from threading import Thread from cloudshell.cli.cli import Cli from cloudshell.cli.command_mode import CommandMode from cloudshell.cli.session.ssh_session import SSHSession from cloudshell.cli.session_pool_manager import SessionPoolManager from cloudshell.core.logger.qs_logger import get_qs_logger CLI_MODE = CommandMode(r'%\s*$', '', 'exit') DEFAULT_MODE = CommandMode(r'>\s*$', 'cli', 'exit', parent_mode=CLI_MODE, default_actions=lambda session, logger: session. hardware_expect(command='set cli screen-length 0', expected_string=r'>\s*$', logger=logger)) CONFIG_MODE = CommandMode(r'#\s*$', 'configure', 'exit', parent_mode=DEFAULT_MODE) def do_action(cli, mode, attrs): session_type = SSHSession with cli.get_session(session_type, attrs, mode, cli.logger) as default_session: out = default_session.send_command('show interfaces', logger=cli.logger) # print(out) # with default_session.enter_mode(CONFIG_MODE) as config_session:
class TestCommandMode(TestCase): def setUp(self): self._prompt = Mock() self._enter_command = Mock() self._enter_action_map = Mock() self._enter_error_map = Mock() self._exit_command = Mock() self._exit_action_map = Mock() self._exit_error_map = Mock() self._enter_actions = Mock() self._session = Mock() self._command_mode = CommandMode(self._prompt, enter_command=self._enter_command, enter_action_map=self._enter_action_map, enter_error_map=self._enter_error_map, exit_command=self._exit_command, exit_action_map=self._exit_action_map, exit_error_map=self._exit_error_map, enter_actions=self._enter_actions) self._logger = Mock() def test_init(self): attributes = ['_enter_error_map', 'prompt', '_enter_command', '_exit_error_map', '_exit_command', 'child_nodes', '_exit_action_map', '_enter_actions', 'parent_node', '_enter_action_map'] self.assertTrue(len(set(attributes).difference(set(self._command_mode.__dict__.keys()))) == 0) def test_add_parent_mode(self): mode = Mock() self._command_mode.add_parent_mode(mode) mode.add_child_node.assert_called_once_with(self._command_mode) def test_step_up_send_command(self): cli_service = Mock() self._command_mode.step_up(cli_service) cli_service.send_command.assert_called_once_with(self._enter_command, expected_string=self._prompt, action_map=self._enter_action_map, error_map=self._enter_error_map) def test_step_up_set_command_mode(self): cli_service = Mock() self._command_mode.step_up(cli_service) self.assertTrue(cli_service.command_mode == self._command_mode) def test_step_up_call_enter_actions(self): cli_service = Mock() enter_actions = Mock() self._command_mode.enter_actions = enter_actions self._command_mode.step_up(cli_service) enter_actions.assert_called_once_with(cli_service) def test_step_down_sent_command(self): cli_service = Mock() parent_prompt = Mock() parent_node = Mock() parent_node.prompt = parent_prompt self._command_mode.parent_node = parent_node self._command_mode.step_down(cli_service) cli_service.send_command.assert_called_once_with(self._exit_command, expected_string=parent_prompt, action_map=self._exit_action_map, error_map=self._exit_error_map) def test_step_down_set_mode(self): cli_service = Mock() parent_prompt = Mock() parent_node = Mock() parent_node.prompt = parent_prompt self._command_mode.parent_node = parent_node self._command_mode.step_down(cli_service) self.assertTrue(cli_service.command_mode == parent_node)