Beispiel #1
0
    def _windows_shell(self, chan: paramiko.channel.Channel) -> None:
        """ Start Windows shell with SSH server

        :param paramiko.channel.Channel chan: channel for communicating with SSH server
        :return None:
        """
        def writeall(sock):
            while True:
                data = sock.recv(256)
                if not data:
                    sys.stdout.flush()
                    return

                sys.stdout.write(data)
                sys.stdout.flush()

        writer = threading.Thread(target=writeall, args=(chan, ))
        writer.start()

        try:
            while True:
                d = sys.stdin.read(1)
                if not d:
                    break

                chan.send(d)

        except Exception as err:
            print_error("Error", err, verbose=self.verbosity)
Beispiel #2
0
 def _posix_shell(channel: paramiko.channel.Channel) -> None:
     """ Start posix shell with SSH server
     :param paramiko.channel.Channel channel: channel for communicating with SSH server
     :return None:
     """
     import termios
     import tty
     old_tty = termios.tcgetattr(sys.stdin)
     try:
         tty.setraw(sys.stdin.fileno())
         tty.setcbreak(sys.stdin.fileno())
         channel.settimeout(0.0)
         while True:
             r, w, e = select.select([channel, sys.stdin], [], [])
             if channel in r:
                 try:
                     x = str(channel.recv(1024), "utf-8")
                     if len(x) == 0:
                         break
                     sys.stdout.write(x)
                     sys.stdout.flush()
                 except socket.timeout:
                     pass
             if sys.stdin in r:
                 x = sys.stdin.read(1)
                 if len(x) == 0:
                     break
                 channel.send(x)
     finally:
         termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty)
         return
Beispiel #3
0
    def _windows_shell(self, chan: paramiko.channel.Channel) -> None:
        """ Start Windows shell with SSH server

        :param paramiko.channel.Channel chan: channel for communicating with SSH server
        :return None:
        """

        def writeall(sock):
            while True:
                data = sock.recv(256)
                if not data:
                    sys.stdout.flush()
                    return

                sys.stdout.write(data)
                sys.stdout.flush()

        writer = threading.Thread(target=writeall, args=(chan,))
        writer.start()

        try:
            while True:
                d = sys.stdin.read(1)
                if not d:
                    break

                chan.send(d)

        except Exception as err:
            print_error("Error", err, verbose=self.verbosity)
Beispiel #4
0
    def _read_ssh_chunks(
        self,
        channel: paramiko.channel.Channel,
        text: bool,
        encoding: str,
        pipe: bool,
    ) -> Tuple[Union[bytes, str], Union[bytes, str]]:
        stdout_chunk = b''  # type: Union[bytes, str]
        stderr_chunk = b''  # type: Union[bytes, str]

        if channel.recv_ready():
            stdout_chunk = channel.recv(len(channel.in_buffer))
        if channel.recv_stderr_ready():
            stderr_chunk = channel.recv_stderr(len(channel.in_stderr_buffer))

        if text:
            stdout_chunk = stdout_chunk.decode(encoding)  # type: ignore
            stderr_chunk = stderr_chunk.decode(encoding)  # type: ignore

        if not pipe:
            print(stdout_chunk, file=sys.stdout, end='')
            print(stderr_chunk, file=sys.stderr, end='')
            stdout_chunk = ''
            stderr_chunk = ''

        return (stdout_chunk, stderr_chunk)
Beispiel #5
0
    def _posix_shell(self, chan: paramiko.channel.Channel) -> None:
        """ Start posix shell with SSH server

        :param paramiko.channel.Channel chan: channel for communicating with SSH server
        :return None:
        """

        import termios
        import tty

        oldtty = termios.tcgetattr(sys.stdin)
        try:
            tty.setraw(sys.stdin.fileno())
            tty.setcbreak(sys.stdin.fileno())
            chan.settimeout(0.0)

            while True:
                r, w, e = select.select([chan, sys.stdin], [], [])
                if chan in r:
                    try:
                        x = str(chan.recv(1024), "utf-8")
                        if len(x) == 0:
                            break

                        sys.stdout.write(x)
                        sys.stdout.flush()
                    except socket.timeout:
                        pass

                if sys.stdin in r:
                    x = sys.stdin.read(1)
                    if len(x) == 0:
                        break
                    chan.send(x)
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
            return