Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # initializes connection
- def login(self):
- self.logger.info("logging in to %s" % self.destination_name)
- self.connection = pxssh.pxssh()
- self.connection.login(server=self.credentials.ip, username=self.credentials.username,
- password=self.credentials.password)
- self.connection.setwinsize(self.connection.maxread, self.connection.maxread)
- def set_cmd_execution_default_values(self):
- self.last_response = None
- self.prompt = self.connection.PROMPT + '|($|>|#)'
- def send_cmd(self, cmd):
- """
- :param cmd: the cmd being sent to the component
- :return: True if cmd was sent, otherwise False
- """
- self.connection.sendline(cmd.cmd_text)
- did_cmd_run = self.connection.prompt(cmd.timeout)
- if not did_cmd_run:
- return False
- self.logger.info("%s was sent successfully" % cmd.cmd_text)
- return True
- def return_connection_to_initial_prompt(self):
- self.connection.PROMPT = self.prompt
- self.connection.prompt()
- def finish_cmd_execution(self, cmd):
- self.logger.info("%s finished executing" % cmd.cmd_text)
- self.is_cmd_running = False
- # executes the requested command
- # The command is sent to it's destination
- def run_cmd_connection_wrapper(self, cmd):
- try:
- self.set_cmd_execution_default_values()
- for i in range(cmd.retries):
- if not self.send_cmd(cmd=cmd):
- continue
- self.filter_response()
- self.logger.info("command return: {0}".format(self.last_response))
- if cmd.regex_action_dict:
- cmd.match_and_execute_action_by_regex(string=self.last_response)
- return self.last_response
- raise CmdNotExecutedInTimeoutException("%s wasn't executed within it's timeout of %s "
- "seconds after %s retries" % (cmd.cmd_text,
- cmd.timeout, cmd.retries))
- def fill_connection_regex(self):
- CONNECTION_REGEX['password:'] = self._complete_connection # handles the standard case of connection
- CONNECTION_REGEX['(yes/no)'] = self._complete_authentication_and_connection # handles first time connections
- CONNECTION_REGEX['not known'] = self._connection_failed # handles connection failure
- if 'password' in CONNECTION_REGEX.keys():
- del CONNECTION_REGEX['password']
- # logs in to source component and then to target component
- def login(self):
- super(NestedConnectionWrapper, self).login()
- self.logger.info("logged into %s, now loggin into %s" % (self.destination_name, self.target_name))
- self.create_nested_connection()
- def create_nested_connection(self):
- cmd_text = self._get_ssh_connection_string()
- self.logger.info("generating and sending ssh command")
- connection_cmd = SshCmd(cmd_text=cmd_text, retries=3, timeout=30, regex_action_dict=CONNECTION_REGEX,
- is_continuous=False)
- self.monitor_cmd(connection_cmd)
- def _get_ssh_connection_string(self):
- return 'ssh %s@%s' % (self.target_credentials.username, self.target_credentials.ip)
Add Comment
Please, Sign In to add comment