Example #1
0
 def __init__(self, command2run, stdin_port, stdout_port):
     self.command2run = command2run
     self.forward_in_sock = create_input_socket_server(port=stdin_port)
     self.forward_out_sock = create_output_socket_publisher(
         port=stdout_port)
     self._done = threading.Event()
     self.__subproc = subprocess.Popen(command2run,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.STDOUT)
     self._out_thread = TillDoneThread(
         target=self.forward_subprocess_output,
         done_event=self._done,
         name="puller",
         kwargs={
             'pulling_done': self._done,
             'sub_process': self.__subproc,
             'forward_sock': self.forward_out_sock
         })
     self._in_thread = TillDoneThread(target=self.forward_subprocess_input,
                                      done_event=self._done,
                                      name="injector",
                                      kwargs={
                                          'pulling_done': self._done,
                                          'sub_process': self.__subproc,
                                          'forward_sock':
                                          self.forward_in_sock
                                      })
     self.start()
Example #2
0
    def open(self):
        """
        Open Ssh channel to remote shell & start thread pulling data from it.

        If SshShell was created with "reused ssh transport" then no new transport is created - just shell channel.
        (such connection establishment is quicker)
        Else - before creating channel we create ssh transport and perform full login with provided credentials.

        May be used as context manager: with connection.open():
        """
        was_closed = self._shell_channel is None
        self.sshshell.open()
        is_open = self._shell_channel is not None
        if was_closed and is_open:
            self._notify_on_connect()
        if self.pulling_thread is None:
            # set reading timeout in same thread where we open shell and before starting pulling thread
            self.sshshell._settimeout(timeout=self.pulling_timeout)
            self._pulling_done.clear()
            self.pulling_thread = TillDoneThread(
                target=self._pull_data,
                done_event=self._pulling_done,
                kwargs={'pulling_done': self._pulling_done})
            self.pulling_thread.start()
        return contextlib.closing(self)
Example #3
0
    def open(self):
        """Open ThreadedTerminal connection & start thread pulling data from it."""
        ret = super(ThreadedTerminal, self).open()

        if not self._terminal:
            self._terminal = PtyProcessUnicode.spawn(
                self._cmd, dimensions=self.dimensions)
            # need to not replace not unicode data instead of raise exception
            self._terminal.decoder = codecs.getincrementaldecoder('utf-8')(
                errors='replace')

            done = Event()
            self.pulling_thread = TillDoneThread(target=self.pull_data,
                                                 done_event=done,
                                                 kwargs={'pulling_done': done})
            self.pulling_thread.start()
            retry = 0
            is_operable = False

            while (retry < 10) and (not is_operable):
                is_operable = self._shell_operable.wait(timeout=1)
                if not is_operable:
                    self.logger.warning(
                        "Terminal open but not fully operable yet.\nREAD_BUFFER: '{}'"
                        .format(self.read_buffer.encode("UTF-8", "replace")))
                    self._terminal.write('\n')
                    retry += 1

        return ret
Example #4
0
 def __init__(self,
              stdin_port,
              stdout_port,
              command='/bin/bash',
              args=None,
              env=None,
              starting_path=None):
     self.command = command
     self.args = [command]  # command have to be arg0
     if args:
         self.args.extend(args)
     self.env = env  # if env == None spawned bash will be given with os.environ
     self.path = starting_path
     self.forward_in_sock = create_input_socket_client(port=stdin_port)
     self.forward_out_sock = create_output_socket_subsciber(
         port=stdout_port)
     self._done = threading.Event()
     self._out_thread = TillDoneThread(target=self.read_subprocess_output,
                                       done_event=self._done,
                                       name="reader",
                                       kwargs={
                                           'reading_done': self._done,
                                           'forward_sock':
                                           self.forward_out_sock
                                       })
     self.start()
Example #5
0
 def open(self):
     """Open TCP connection & start thread pulling data from it."""
     super(ThreadedTcp, self).open()
     done = threading.Event()
     self.pulling_thread = TillDoneThread(target=self.pull_data,
                                          done_event=done,
                                          kwargs={'pulling_done': done})
     self.pulling_thread.start()
Example #6
0
 def open(self):
     """Start thread pulling data from FIFO buffer."""
     super(ThreadedFifoBuffer, self).open()
     done = threading.Event()
     self.pulling_thread = TillDoneThread(target=self.pull_data,
                                          done_event=done,
                                          kwargs={'pulling_done': done})
     self.pulling_thread.start()
Example #7
0
 def open(self):
     """Start thread pulling data from FIFO buffer."""
     super(ThreadedFifoBuffer, self).open()
     done = threading.Event()
     self.pulling_thread = TillDoneThread(target=self.pull_data,
                                          done_event=done,
                                          kwargs={'pulling_done': done})
     self.pulling_thread.start()
     self._log(msg="open {}".format(self), level=logging.INFO)
     self._notify_on_connect()
Example #8
0
 def open(self):
     """Open ThreadedTerminal connection & start thread pulling data from it."""
     if not self._terminal:
         self._terminal = PtyProcessUnicode.spawn(
             self._cmd, dimensions=self.dimensions)
         done = Event()
         self.pulling_thread = TillDoneThread(target=self.pull_data,
                                              done_event=done,
                                              kwargs={'pulling_done': done})
         self.pulling_thread.start()
         self._shell_operable.wait(timeout=2)