Guest User

Untitled

a guest
Feb 8th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. # initializes connection
  2. def login(self):
  3. self.logger.info("logging in to %s" % self.destination_name)
  4. self.connection = pxssh.pxssh()
  5. self.connection.login(server=self.credentials.ip, username=self.credentials.username,
  6. password=self.credentials.password)
  7. self.connection.setwinsize(self.connection.maxread, self.connection.maxread)
  8.  
  9. def set_cmd_execution_default_values(self):
  10. self.last_response = None
  11. self.prompt = self.connection.PROMPT + '|($|>|#)'
  12.  
  13. def send_cmd(self, cmd):
  14. """
  15. :param cmd: the cmd being sent to the component
  16. :return: True if cmd was sent, otherwise False
  17. """
  18. self.connection.sendline(cmd.cmd_text)
  19. did_cmd_run = self.connection.prompt(cmd.timeout)
  20. if not did_cmd_run:
  21. return False
  22. self.logger.info("%s was sent successfully" % cmd.cmd_text)
  23. return True
  24.  
  25. def return_connection_to_initial_prompt(self):
  26. self.connection.PROMPT = self.prompt
  27. self.connection.prompt()
  28.  
  29. def finish_cmd_execution(self, cmd):
  30. self.logger.info("%s finished executing" % cmd.cmd_text)
  31. self.is_cmd_running = False
  32.  
  33. # executes the requested command
  34. # The command is sent to it's destination
  35. def run_cmd_connection_wrapper(self, cmd):
  36. try:
  37. self.set_cmd_execution_default_values()
  38. for i in range(cmd.retries):
  39. if not self.send_cmd(cmd=cmd):
  40. continue
  41. self.filter_response()
  42. self.logger.info("command return: {0}".format(self.last_response))
  43. if cmd.regex_action_dict:
  44. cmd.match_and_execute_action_by_regex(string=self.last_response)
  45. return self.last_response
  46. raise CmdNotExecutedInTimeoutException("%s wasn't executed within it's timeout of %s "
  47. "seconds after %s retries" % (cmd.cmd_text,
  48. cmd.timeout, cmd.retries))
  49.  
  50. def fill_connection_regex(self):
  51. CONNECTION_REGEX['password:'] = self._complete_connection # handles the standard case of connection
  52. CONNECTION_REGEX['(yes/no)'] = self._complete_authentication_and_connection # handles first time connections
  53. CONNECTION_REGEX['not known'] = self._connection_failed # handles connection failure
  54. if 'password' in CONNECTION_REGEX.keys():
  55. del CONNECTION_REGEX['password']
  56.  
  57. # logs in to source component and then to target component
  58. def login(self):
  59. super(NestedConnectionWrapper, self).login()
  60. self.logger.info("logged into %s, now loggin into %s" % (self.destination_name, self.target_name))
  61. self.create_nested_connection()
  62.  
  63. def create_nested_connection(self):
  64. cmd_text = self._get_ssh_connection_string()
  65. self.logger.info("generating and sending ssh command")
  66. connection_cmd = SshCmd(cmd_text=cmd_text, retries=3, timeout=30, regex_action_dict=CONNECTION_REGEX,
  67. is_continuous=False)
  68. self.monitor_cmd(connection_cmd)
  69.  
  70. def _get_ssh_connection_string(self):
  71. return 'ssh %s@%s' % (self.target_credentials.username, self.target_credentials.ip)
Add Comment
Please, Sign In to add comment