def __init__(self, connection, host, login=None, password=None, port=0, prompt=None, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", prefix=None, newline_chars=None, cmds_before_establish_connection=[], cmds_after_establish_connection=[], telnet_prompt=r"^\s*telnet>\s*", encrypt_password=True, runner=None, target_newline="\r\n"): super(Telnet, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt(expected_prompt) # Expected prompt on device self._re_telnet_prompt = CommandTextualGeneric._calculate_prompt(telnet_prompt) # Prompt for telnet commands self.login = login self.password = password self.host = host self.port = port self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.prefix = prefix self.encrypt_password = encrypt_password self.cmds_before_establish_connection = copy.deepcopy(cmds_before_establish_connection) self.cmds_after_establish_connection = copy.deepcopy(cmds_after_establish_connection) self.target_newline = target_newline # Internal variables self._sent_timeout = False self._sent_prompt = False self._sent_login = False self._sent_password = False self._telnet_command_mode = False
def __init__(self, connection, prompt, expected_prompt, newline_chars=None, runner=None, set_timeout=None, set_prompt=None, target_newline="\n", allowed_newline_after_prompt=False, prompt_after_login=None): """ Moler base class for commands that change prompt. :param connection: moler connection to device, terminal when command is executed. :param prompt: prompt on start system (where command starts). :param expected_prompt: prompt on server (where command connects). :param newline_chars: characters to split lines. :param runner: Runner to run command. :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1 then leave it None. """ super(CommandChangingPrompt, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self._re_prompt_after_login = self._re_expected_prompt if prompt_after_login: self._re_prompt_after_login = CommandTextualGeneric._calculate_prompt( prompt_after_login) self.set_timeout = set_timeout self.set_prompt = set_prompt self.target_newline = target_newline self.allowed_newline_after_prompt = allowed_newline_after_prompt self.enter_on_prompt_without_anchors = True # Set True to try to match prompt in line without ^ and $. # Internal variables self._re_failure_exceptions_indication = None self._sent_timeout = False self._sent_prompt = False self._sent = False self._finish_on_final_prompt = True # Set True to finish Moler command by this generic after prompt after # command output. False if you want to finish command in your class. self._re_expected_prompt_without_anchors = regexp_without_anchors( self._re_expected_prompt) self._re_prompt_after_login_without_anchors = regexp_without_anchors( self._re_prompt_after_login)
def _is_prompt(self, line): if self.expected_prompt: self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( self.expected_prompt) return self._regex_helper.search_compiled(self._re_expected_prompt, line) return False
def __init__(self, connection, newline_chars=None, prompt=None, runner=None, expected_prompt=r'moler_bash#', set_timeout=None, set_prompt=None, target_newline="\n", allowed_newline_after_prompt=False, prompt_after_login=None): """ Class for exit telnet command. :param connection: moler connection to device, terminal when command is executed. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param newline_chars: characters to split lines. :param runner: Runner to run command. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1 then leave it None. """ super(ExitTelnet, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner, expected_prompt=expected_prompt, set_timeout=set_timeout, set_prompt=set_prompt, target_newline=target_newline, allowed_newline_after_prompt=allowed_newline_after_prompt, prompt_after_login=prompt_after_login) self.target_newline = target_newline self.ret_required = False self._command_sent = False self._re_expected_prompt = CommandTextualGeneric._calculate_prompt(expected_prompt) self.newline_after_command_string = False
def __init__(self, connection, path=None, prompt=None, newline_chars=None, runner=None, expected_prompt=None): """ :param connection: moler connection to device :param prompt: start prompt (on system where command cd starts) :param path: path to directory :param expected_prompt: Prompt after change directory :param newline_chars: Characters to split lines :param runner: Runner to run command """ super(Cd, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self.path = path # command parameters self.ret_required = False self._re_expected_prompt = None if expected_prompt: self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device
def __init__(self, connection, login, password, host, prompt=None, expected_prompt='>', port=0, known_hosts_on_failure='keygen', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", newline_chars=None, encrypt_password=True, runner=None, target_newline="\n"): """ :param connection: moler connection to device, terminal when command is executed :param login: ssh login :param password: ssh password :param host: host to ssh :param prompt: start prompt (on system where command ssh starts) :param expected_prompt: final prompt (on system where command ssh connects) :param port: port to ssh connect :param known_hosts_on_failure: "rm" or "keygen" how to deal with error. If empty then ssh fails. :param set_timeout: Command to set timeout after ssh connects :param set_prompt: Command to set prompt after ssh connects :param term_mono: Params to set ssh mono connection (useful in script) :param newline_chars: Characters to split lines :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text :param runner: Runner to run command :param target_newline: newline chars on remote system where ssh connects """ super(Ssh, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self.login = login self.password = password self.host = host self.port = port self.known_hosts_on_failure = known_hosts_on_failure self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.encrypt_password = encrypt_password self.target_newline = target_newline self.ret_required = False # Internal variables self._hosts_file = "" self._sent_timeout = False self._sent_prompt = False self._sent_password = False self._sent_continue_connecting = False
def __init__(self, connection, login, password, host, prompt=None, expected_prompt='>', port=0, known_hosts_on_failure='keygen', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", new_line_chars=None): super(Ssh, self).__init__(connection, prompt, new_line_chars) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt(expected_prompt) # Expected prompt on device self.login = login self.password = password self.host = host self.port = port self.known_hosts_on_failure = known_hosts_on_failure self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.ret_required = False # Internal variables self._hosts_file = "" self._sent_timeout = False self._sent_prompt = False self._sent_password = False self._sent_continue_connecting = False
def __init__(self, connection, login=None, options=None, password=None, prompt=None, expected_prompt=None, newline_chars=None, encrypt_password=True, target_newline="\n", runner=None, set_timeout=None, allowed_newline_after_prompt=False, set_prompt=None): """ Moler class of Unix command su. :param connection: moler connection to device, terminal when command is executed. :param login: user name. :param options: su unix command options. :param password: password. :param prompt: start prompt. :param expected_prompt: final prompt. :param newline_chars: Characters to split lines. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text. :param target_newline: newline chars on root user. :param runner: Runner to run command. :param set_timeout: Command to set timeout after su success. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt :param set_prompt: Command to set prompt after su success. """ super(Su, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self.expected_prompt = expected_prompt self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self.login = login self.options = options self.password = password self.encrypt_password = encrypt_password self.target_newline = target_newline self.set_timeout = set_timeout self.allowed_newline_after_prompt = allowed_newline_after_prompt self.set_prompt = set_prompt # Internal variables self._sent_password = False self._sent_timeout = False self._sent_prompt = False self.current_ret = dict() self.current_ret['RESULT'] = list()
def _is_prompt(self, line): """ Checks if device sends final prompt. :param line: Line from device. :return: Match object if regex matches, None otherwise """ if self.expected_prompt: self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( self.expected_prompt) return self._regex_helper.search_compiled(self._re_expected_prompt, line) return None
def __init__(self, connection, prompt=None, expected_prompt='>', newline_chars=None, runner=None, target_newline="\n"): """ :param connection: :param prompt: Prompt of the starting shell :param expected_prompt: Prompt of the target shell reached after exit command :param newline_chars: """ super(Exit, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) self.ret_required = False self.target_newline = target_newline # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt(expected_prompt) # Expected prompt on device
def __init__(self, connection, prompt=None, newline_chars=None, expected_prompt=r'moler_bash#', runner=None, target_newline="\n"): """ :param connection: :param prompt: Prompt of the starting shell :param expected_prompt: Prompt of the target shell reached after quit command :param newline_chars: """ super(ExitTelnet, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) self.target_newline = target_newline self.ret_required = False self._command_sent = False self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt)
def __init__(self, connection, prompt=None, expected_prompt=None, newline_chars=None, runner=None): """ Unix ctrl+c command :param connection: Moler connection to device, terminal when command is executed. :param prompt: Prompt of the starting shell :param expected_prompt: Prompt of the target shell reached after ctrl+c command :param newline_chars: Characters to split lines - list. :param runner: Runner to run command. """ super(CtrlC, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) self.ret_required = False self.break_on_timeout = False # If True then Ctrl+c on timeout self.newline_after_command_string = False # Do not send newline after command string self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device
def __init__(self, connection, host, login=None, password=None, port=0, prompt=None, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", prefix=None, newline_chars=None, cmds_before_establish_connection=None, cmds_after_establish_connection=None, telnet_prompt=r"^\s*telnet>\s*", encrypt_password=True, runner=None, target_newline="\n", allowed_newline_after_prompt=False): """ Moler class of Unix command telnet. :param connection: moler connection to device, terminal when command is executed :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param prefix: prefix telnet command. :param newline_chars: characters to split lines. :param cmds_before_establish_connection: list of commands to execute by telnet command before open. :param cmds_after_establish_connection: list of commands to execute by telnet commands after establishing. :param telnet_prompt: prompt for telnet commands. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text :param runner: Runner to run command :param target_newline: newline chars on remote system where ssh connects :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt """ super(Telnet, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self._re_telnet_prompt = CommandTextualGeneric._calculate_prompt( telnet_prompt) # Prompt for telnet commands self.login = login self.password = password self.host = host self.port = port self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.prefix = prefix self.encrypt_password = encrypt_password self.cmds_before_establish_connection = copy_list( cmds_before_establish_connection) self.cmds_after_establish_connection = copy_list( cmds_after_establish_connection) self.target_newline = target_newline self.allowed_newline_after_prompt = allowed_newline_after_prompt # Internal variables self._sent_timeout = False self._sent_prompt = False self._sent_login = False self._sent_password = False self._telnet_command_mode = False
def __init__(self, connection, host, login=None, password=None, newline_chars=None, prompt=None, runner=None, port=0, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", encrypt_password=True, target_newline="\n", allowed_newline_after_prompt=False, repeat_password=True, failure_exceptions_indication=None, prompt_after_login=None, send_enter_after_connection=True, username=None): """ Base Moler class of Unix commands telnet and ssh. :param connection: moler connection to device, terminal when command is executed. :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param newline_chars: characters to split lines. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text. :param runner: Runner to run command. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param repeat_password: If True then repeat last password if no more provided. If False then exception is set. :param failure_exceptions_indication: String with regex or regex object to omit failure even if failed string was found. :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1 then leave it None. :param send_enter_after_connection: set True to send new line char(s) after connection is established, False otherwise. """ super(GenericTelnetSsh, self).__init__( connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner, expected_prompt=expected_prompt, set_timeout=set_timeout, set_prompt=set_prompt, target_newline=target_newline, allowed_newline_after_prompt=allowed_newline_after_prompt, prompt_after_login=prompt_after_login) self.timeout = 90 # Parameters defined by calling the command self._re_failure_exceptions_indication = None if failure_exceptions_indication: self._re_failure_exceptions_indication = CommandTextualGeneric._calculate_prompt( failure_exceptions_indication) self.login = login if isinstance(password, six.string_types): self._passwords = [password] elif password is None: self._passwords = [] else: self._passwords = copy_list( password, deep_copy=False) # copy of list of passwords to modify self.host = host self.port = port self.term_mono = term_mono self.encrypt_password = encrypt_password self.repeat_password = repeat_password self.send_enter_after_connection = send_enter_after_connection # Internal variables self._sent_login = False self._last_password = "" if login and username: self.command_string = self.__class__.__name__ raise CommandFailure( self, "Please set login ('{}') or username ('{}') but not both.". format(login, username)) elif username: self.login = username self.current_ret['LINES'] = list() self.current_ret['LAST_LOGIN'] = dict() self.current_ret['FAILED_LOGIN_ATTEMPTS'] = None self._converter_helper = ConverterHelper.get_converter_helper()
def __init__(self, connection, host, login=None, password=None, newline_chars=None, prompt=None, runner=None, port=0, expected_prompt=r'^>\s*', set_timeout=r'export TMOUT=\"2678400\"', set_prompt=None, term_mono="TERM=xterm-mono", encrypt_password=True, target_newline="\n", allowed_newline_after_prompt=False, repeat_password=True, failure_exceptions_indication=None): """ Base Moler class of Unix commands telnet and ssh. :param connection: moler connection to device, terminal when command is executed. :param host: address of telnet server. :param login: login to telnet server. :param password: password to telnet server. :param port: port to listen on server. :param prompt: prompt on start system (where command telnet starts). :param expected_prompt: prompt on server (where command telnet connects). :param set_timeout: Command to set timeout after telnet connects. :param set_prompt: Command to set prompt after telnet connects. :param term_mono: Params to set ssh mono connection (useful in script). :param newline_chars: characters to split lines. :param encrypt_password: If True then * will be in logs when password is sent, otherwise plain text. :param runner: Runner to run command. :param target_newline: newline chars on remote system where ssh connects. :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt. :param repeat_password: If True then repeat last password if no more provided. If False then exception is set. :param failure_exceptions_indication: String with regex or regex object to omit failure even if failed string was found. """ super(GenericTelnetSsh, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner) # Parameters defined by calling the command self._re_expected_prompt = CommandTextualGeneric._calculate_prompt( expected_prompt) # Expected prompt on device self._re_failure_exceptions_indication = None if failure_exceptions_indication: self._re_failure_exceptions_indication = CommandTextualGeneric._calculate_prompt( failure_exceptions_indication) self.login = login if isinstance(password, six.string_types): self._passwords = [password] elif password is None: self._passwords = [] else: self._passwords = copy_list( password, deep_copy=False) # copy of list of passwords to modify self.host = host self.port = port self.set_timeout = set_timeout self.set_prompt = set_prompt self.term_mono = term_mono self.encrypt_password = encrypt_password self.target_newline = target_newline self.allowed_newline_after_prompt = allowed_newline_after_prompt self.repeat_password = repeat_password # Internal variables self._sent_timeout = False self._sent_prompt = False self._sent_login = False self._sent = False self._last_password = ""