示例#1
0
    def loop(self):
        chan = self.chan
        master_fd = self.master_fd
        fds = [master_fd, chan]
        while master_fd or chan.active:
            try:
                rfds, wfds, xfds = select.select(fds, [], [], 5)
            except:
                log.exception('Exception in PTYWrapper.loop():')
                break

            if master_fd in rfds:
                try:
                    data = pty._read(master_fd)
                except OSError:
                    break
                if data == '':
                    break
                chan.send(data)
            if chan in rfds:
                data = chan.recv(10240)
                if data == '':
                    break
                pty._writen(master_fd, data)
                if chan.closed or chan.eof_received:
                    del fds[fds.index(chan)]
                    pty._writen(master_fd, '\004')  # CTRL-D
                    continue
示例#2
0
def _copy(fd, observe, act):
    while True:
        buf = _drain(fd)
        if buf:
            observe(buf)
            os.write(1, buf)
        pty._writen(fd, act().encode('utf-8'))
示例#3
0
def _copy(fd, observe, act):
    while True:
        buf = _drain(fd)
        if buf:
            observe(buf)
            os.write(1, buf)
        pty._writen(fd, act().encode('utf-8'))
示例#4
0
文件: pity.py 项目: abenassi/rlundo
def _copy(master_fd, master_read=pty._read, stdin_read=pty._read):
    """Parent copy loop.
    Copies
            pty master -> standard output   (master_read)
            standard input -> pty master    (stdin_read)"""
    logging.debug('starting _copy loop')
    fds = [master_fd, STDIN_FILENO]
    while True:
        logging.debug('calling select in copy')
        rfds, wfds, xfds = select.select(fds, [], [])
        logging.debug('select call in copy finished! %r %r %r' % (rfds, wfds, xfds, ))
        if master_fd in rfds:
            logging.debug('master_fd is ready, so calling read')
            data = master_read(master_fd)
            logging.debug('master_fd master_read call done, got data: %r' % (data, ))
            if not data:  # Reached EOF.
                fds.remove(master_fd)
            else:
                os.write(STDOUT_FILENO, data)
        if STDIN_FILENO in rfds:
            logging.debug('stdin is ready, dealing...')

            with Nonblocking(STDIN_FILENO):
                try:
                    data = stdin_read(STDIN_FILENO)
                except OSError as e:
                    if e[0] != errno.EAGAIN:
                        raise
                else:
                    if not data:
                        fds.remove(STDIN_FILENO)
                    else:
                        pty._writen(master_fd, data)
            logging.debug('done dealing with stdin')
示例#5
0
    def parse_resp(self,buf):

        header_offset = 2
        avp_offset = 8
        nr = 0
        ns = 0

        # read the header
        (cflag,) = struct.unpack_from('!H', buf)

        cflag_bin = int2bin(cflag,16)
        ptype = cflag_bin[0]
        blen = cflag_bin[1]
        sbit = cflag_bin[4]
        obit = cflag_bin[6]
        pbit = cflag_bin[7]
        ver  = cflag_bin[12:16]

        if self.debug:
            print "<- l2tp packet dump"
            print "<-: l2tp cflag bits : %s|%s|%s|%s|%s|%s" % (ptype, blen, sbit, obit, pbit, ver)

        if ver != '0010': #
            print '!! Not an valid l2tp packet : discarding'
            return None

        if blen == '1':
            (plen,) = struct.unpack_from('!H', buf, offset=header_offset)
            if self.debug:
                print "<-: l2tp length : %d" % plen
            header_offset += 2

        (tid, sid) = struct.unpack_from('!HH', buf, offset=header_offset)
        if self.debug:
            print "<-: l2tp tunnel_id : %d, session_id : %d" % (tid, sid)
        header_offset += 4

        if sbit == '1':
            (ns, nr) = struct.unpack_from('!HH', buf, offset=header_offset)
            if self.debug:
                print "<-: l2tp ns : %d, nr : %d" % (ns, nr)
            header_offset += 4
            avp_offset += 4

        if obit == '1':
            (offset_size, offset_pad) = struct.unpack_from('!HH', buf, offset=header_offset)
            if self.debug:
                print "<-: l2tp offset_size : %d, offset_pad : %d" % (offset_size, offset_pad)
            header_offset += 4
            avp_offset += 4

        if ptype == '0': # data packet
            # write to pppd
            data = buf[header_offset:]
            try:
                async_buf = self.pppd_sync_to_async(data)
                pty._writen(self.pppd_fd, async_buf)
            except OSError, se:
                if se.args[0] not in (errno.EAGAIN, errno.EINTR):
                    raise
示例#6
0
 def loop(self):
     chan = self.chan
     master_fd = self.master_fd
     fds = [master_fd, chan]
     while master_fd or chan.active:
         try:
             rfds, wfds, xfds = select.select(
                     fds, [], [], 5)
         except:
             log.exception('Exception in PTYWrapper.loop():')
             break
         
         if master_fd in rfds:
             try:
                 data = pty._read(master_fd)
             except OSError:
                 break
             if data == '':
                 break
             chan.send(data)
         if chan in rfds:
             data = chan.recv(10240)
             if data == '':
                 break
             pty._writen(master_fd, data)
             if chan.closed or chan.eof_received:
                 del fds[fds.index(chan)]
                 pty._writen(master_fd, '\004') # CTRL-D
                 continue
def _copy(master_fd, master_read=_read, stdin_read=_read):
    """Parent copy loop.
    
            pty master -> standard output   (master_read)
            pty master -> telegram output   (stdout_bot.sendall)
            standard input -> pty master    (stdin_read)
            telegram input -> pty master    (conn_stdin.recv)"""
    fds = [master_fd, STDIN_FILENO, STDIN_BOT_FILENO, SHUTDOWN_FILENO]

    while True:
        rfds, wfds, xfds = select(fds, [], [])
        if master_fd in rfds:
            data = master_read(master_fd)
            if not data:  # Reached EOF.
                fds.remove(master_fd)
            else:
                os.write(STDOUT_FILENO, data)
                stdout_bot.sendall(
                    escape_ansi(data))  # send output to telegram

        if STDIN_FILENO in rfds:
            data = stdin_read(STDIN_FILENO)
            if not data:
                fds.remove(STDIN_FILENO)
            else:
                _writen(master_fd, data)

        if STDIN_BOT_FILENO in rfds:
            data = conn_stdin.recv(4096)  # read from telegram
            _writen(master_fd, data)

        if SHUTDOWN_FILENO in rfds:
            break
示例#8
0
 def pollInEvent(self):
     try:
         if self.linkedchannel is None:
             return
         try:
             data = self.fd.read()
             written = self.linkedchannel.write_stdout_server(data)
             return written
         except EOFError:
             self.session.server_channels_to_close.append((self.linkedchannel, "OK"))
             pty._writen(self.fd, "exit\n")
             self.fd.close()
     except Exception, e:
         Logger().debug("WABConsole.pollInEvent :: %s" % traceback.format_exc(e))
示例#9
0
def _copy(master_fd,
          master_read=pty._read,
          stdin_read=pty._read,
          terminal_output_lock=None):
    """Parent copy loop.
    Copies
            pty master -> standard output   (master_read)
            standard input -> pty master    (stdin_read)"""
    logging.debug('starting _copy loop')
    fds = [master_fd, STDIN_FILENO]
    while True:
        logging.debug('calling select in copy')
        rfds, wfds, xfds = select.select(fds, [], [])
        logging.debug('select call in copy finished! %r %r %r' % (
            rfds,
            wfds,
            xfds,
        ))
        if master_fd in rfds:
            logging.debug('master_fd is ready, so calling read')
            data = master_read(master_fd)
            logging.debug('master_fd master_read call done, got data: %r' %
                          (data, ))
            if not data:  # Reached EOF.
                fds.remove(master_fd)
            else:
                os.write(STDOUT_FILENO, data)
                if terminal_output_lock is not None:
                    terminal_output_lock.release()

        if STDIN_FILENO in rfds:
            logging.debug('stdin is ready, dealing...')

            with Nonblocking(STDIN_FILENO):
                try:
                    data = stdin_read(STDIN_FILENO)
                except OSError as e:
                    if e.errno != errno.EAGAIN:
                        raise
                else:
                    if not data:
                        fds.remove(STDIN_FILENO)
                    else:
                        if terminal_output_lock is not None:
                            terminal_output_lock.acquire()
                        pty._writen(master_fd, data)
            logging.debug('done dealing with stdin')
示例#10
0
def my_copy_2(master_fd, master_read=pty._read, stdin_read=pty._read):
    """Parent copy loop.
    Copies
            pty master -> standard output   (master_read)
            standard input -> pty master    (stdin_read)"""
    fds = [master_fd, STDIN_FILENO]

    my_data = ["i"]
    for char in list("This is a test!"):
        my_data.append(char)

    os.write(STDOUT_FILENO, "i".encode())
    print("toto")
    while True:
        for char in my_data:
            rfds, wfds, xfds = select(fds, [], [])
            if master_fd in rfds:
                data = master_read(master_fd)
                if not data:  # Reached EOF.
                    fds.remove(master_fd)
                else:
                    os.write(STDOUT_FILENO, data)

            if STDIN_FILENO in rfds:
                data = char.encode()
                time.sleep(.1)
                os.write(master_fd, data)
                time.sleep(.1)

        rfds, wfds, xfds = select(fds, [], [])
        if master_fd in rfds:
            data = master_read(master_fd)
            if not data:  # Reached EOF.
                fds.remove(master_fd)
            else:
                os.write(STDOUT_FILENO, data)

        # Should try removing this part to make it stop waiting
        if STDIN_FILENO in rfds:
            data = stdin_read(STDIN_FILENO)
            if not data:
                fds.remove(STDIN_FILENO)
            else:
                pty._writen(master_fd, data)
示例#11
0
def spawn(argv, master_read, stdin_read, commands_to_run, timeout):
    """Create a spawned process."""
    if type(argv) == type(''):
        argv = (argv, )
    pid, master_fd = pty.fork()
    if pid == CHILD:
        os.execlp(argv[0], *argv)

    pty._writen(master_fd, commands_to_run)

    fds = [master_fd]
    while True:
        rfds, wfds, xfds = select(fds, [], [], timeout)
        if not rfds:  # timeout
            break
        data = master_read(master_fd)
        if not data:  # Reached EOF.
            break

    os.close(master_fd)
示例#12
0
def spawn(argv, master_read, stdin_read):
    """Create a spawned process."""
    if type(argv) == type(''):
        argv = (argv,)
    pid, master_fd = pty.fork()
    if pid == CHILD:
        os.execlp(argv[0], *argv)
    try:
        mode = tty.tcgetattr(STDIN_FILENO)
        tty.setraw(STDIN_FILENO)
        restore = 1
    except tty.error:    # This is the same as termios.error
        restore = 0
    try:
        pty._writen(master_fd, commands_to_run)
        _copy(master_fd, master_read, stdin_read)
    finally:
        if restore:
            tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
    os.close(master_fd)
示例#13
0
def spawn(argv, master_read, stdin_read, commands_to_run, timeout):
    """Create a spawned process."""
    if type(argv) == type(''):
        argv = (argv,)
    pid, master_fd = pty.fork()
    if pid == CHILD:
        os.execlp(argv[0], *argv)

    pty._writen(master_fd, commands_to_run)

    fds = [master_fd]
    while True:
        rfds, wfds, xfds = select(fds, [], [], timeout)
        if not rfds:  # timeout
            break
        data = master_read(master_fd)
        if not data:  # Reached EOF.
            break

    os.close(master_fd)
示例#14
0
文件: l2tpclient.py 项目: ac0ra/cpa
    def parse_resp(self, buf):

        header_offset = 2
        avp_offset = 8
        nr = 0
        ns = 0

        # read the header
        (cflag, ) = struct.unpack_from('!H', buf)

        cflag_bin = int2bin(cflag, 16)
        ptype = cflag_bin[0]
        blen = cflag_bin[1]
        sbit = cflag_bin[4]
        obit = cflag_bin[6]
        pbit = cflag_bin[7]
        ver = cflag_bin[12:16]

        if self.debug:
            print "<- l2tp packet dump"
            print "<-: l2tp cflag bits : %s|%s|%s|%s|%s|%s" % (
                ptype, blen, sbit, obit, pbit, ver)

        if ver != '0010':  #
            print '!! Not an valid l2tp packet : discarding'
            return None

        if blen == '1':
            (plen, ) = struct.unpack_from('!H', buf, offset=header_offset)
            if self.debug:
                print "<-: l2tp length : %d" % plen
            header_offset += 2

        (tid, sid) = struct.unpack_from('!HH', buf, offset=header_offset)
        if self.debug:
            print "<-: l2tp tunnel_id : %d, session_id : %d" % (tid, sid)
        header_offset += 4

        if sbit == '1':
            (ns, nr) = struct.unpack_from('!HH', buf, offset=header_offset)
            if self.debug:
                print "<-: l2tp ns : %d, nr : %d" % (ns, nr)
            header_offset += 4
            avp_offset += 4

        if obit == '1':
            (offset_size,
             offset_pad) = struct.unpack_from('!HH', buf, offset=header_offset)
            if self.debug:
                print "<-: l2tp offset_size : %d, offset_pad : %d" % (
                    offset_size, offset_pad)
            header_offset += 4
            avp_offset += 4

        if ptype == '0':  # data packet
            # write to pppd
            data = buf[header_offset:]
            try:
                async_buf = self.pppd_sync_to_async(data)
                pty._writen(self.pppd_fd, async_buf)
            except OSError, se:
                if se.args[0] not in (errno.EAGAIN, errno.EINTR):
                    raise
示例#15
0
 def close(self):
     pty._writen(self.fd, "exit\n")
     self.console.close_linked_channel()
示例#16
0
 def write(self, data):
     return pty._writen(self.fd, data)
示例#17
0
 def pollErrEvent(self):
     self.session.server_channels_to_close.append((self.linkedchannel, "OK"))
     pty._writen(self.fd, "exit\n")
     self.fd.close()