Beispiel #1
0
 def test_write (self):
     s = ANSI.ANSI (6,65)
     s.fill('.')
     s.cursor_home()
     for c in write_text:
         s.write (c)
     assert str(s) == write_target
Beispiel #2
0
 def test_torturet(self):
     s = ANSI.ANSI(24, 80)
     sample_text = open('torturet.vt').read()
     for c in sample_text:
         s.process(c)
     assert s.pretty() == torture_target, 'processed: \n' + s.pretty(
     ) + '\nexpected:\n' + torture_target
Beispiel #3
0
    def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"):
        self.child = pexpect.spawn(engine)
        self.term = ANSI.ANSI()

        #		self.child.expect ('Chess')
        #	if self.child.after != 'Chess':
        #		raise IOError, 'incompatible chess program'
        #        self.term.process_list (self.before)
        #        self.term.process_list (self.after)
        self.last_computer_move = ''
Beispiel #4
0
 def __init__(self, ip=None):
     self.buffer = ANSI.ANSI(self.Rows, self.Cols)
     self.record = file('record.txt', 'wb')
     self.drac_ip = ip
     self.ControllerCards = {
         'SAS': ('C', 3, 'PercH200Controller'),
         'PERC': ('R', 18, 'Perc6_i_Controller'),
     }
     try:
         reset_conn(self)
     except socket.error, e:
         print "Error in serial connection ", str(e)
         self.conn.close()
         del self.conn
         #self.conn.shutdown(1)
         for i in xrange(10):
             if not self.thread.killed and self.thread.isAlive():
                 self.thread.kill()
         time.sleep(10)
         reset_conn(self)
    if '--username' in options:
        username = options['--username']
    print "Login for %s@%s:%s" % (username, hostname, port)
    if '--password' in options:
        password = options['--password']
    else:
        password = getpass.getpass('password: '******'/dev/null','/tmp/daemon.log','/tmp/daemon.log')

    sys.stdout.write('server started with pid %d\n' % os.getpid())

    virtual_screen = ANSI.ANSI(24, 80)
    child = pxssh.pxssh()
    child.login(hostname, username, password)
    print 'created shell. command line prompt is', child.PROMPT
    #child.sendline ('stty -echo')
    #child.setecho(False)
    virtual_screen.write(child.before)
    virtual_screen.write(child.after)

    if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    localhost = '127.0.0.1'
    s.bind('/tmp/mysock')
    os.chmod('/tmp/mysock', 0777)
    print 'Listen'
    s.listen(1)
Beispiel #6
0
def main():

    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'h?d', [
            'help', 'h', '?', 'hostname=', 'username='******'password='******'port=',
            'watch'
        ])
    except Exception as e:
        print str(e)
        exit_with_usage()

    command_line_options = dict(optlist)
    options = dict(optlist)
    # There are a million ways to cry for help. These are but a few of them.
    if [
            elem for elem in command_line_options
            if elem in ['-h', '--h', '-?', '--?', '--help']
    ]:
        exit_with_usage(0)

    hostname = "127.0.0.1"
    port = 1664
    username = os.getenv('USER')
    password = ""
    daemon_mode = False
    if '-d' in options:
        daemon_mode = True
    if '--watch' in options:
        watch_mode = True
    else:
        watch_mode = False
    if '--hostname' in options:
        hostname = options['--hostname']
    if '--port' in options:
        port = int(options['--port'])
    if '--username' in options:
        username = options['--username']
    print "Login for %s@%s:%s" % (username, hostname, port)
    if '--password' in options:
        password = options['--password']
    else:
        password = getpass.getpass('password: '******'/dev/null','/tmp/daemon.log','/tmp/daemon.log')

    sys.stdout.write('server started with pid %d\n' % os.getpid())

    virtual_screen = ANSI.ANSI(24, 80)
    child = pxssh.pxssh()
    child.login(hostname, username, password)
    print 'created shell. command line prompt is', child.PROMPT
    #child.sendline ('stty -echo')
    # child.setecho(False)
    virtual_screen.write(child.before)
    virtual_screen.write(child.after)

    if os.path.exists("/tmp/mysock"):
        os.remove("/tmp/mysock")
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    localhost = '127.0.0.1'
    s.bind('/tmp/mysock')
    os.chmod('/tmp/mysock', 0o777)
    print 'Listen'
    s.listen(1)
    print 'Accept'
    #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #localhost = '127.0.0.1'
    #s.bind((localhost, port))
    # print 'Listen'
    # s.listen(1)

    r = roller(0.01, endless_poll, (child, child.PROMPT, virtual_screen))
    r.start()
    print "screen poll updater started in background thread"
    sys.stdout.flush()

    try:
        while True:
            conn, addr = s.accept()
            print 'Connected by', addr
            data = conn.recv(1024)
            if data[0] != ':':
                cmd = ':sendline'
                arg = data.strip()
            else:
                request = data.split(' ', 1)
                if len(request) > 1:
                    cmd = request[0].strip()
                    arg = request[1].strip()
                else:
                    cmd = request[0].strip()
            if cmd == ':exit':
                r.cancel()
                break
            elif cmd == ':sendline':
                child.sendline(arg)
                # child.prompt(timeout=2)
                time.sleep(0.2)
                shell_window = str(virtual_screen)
            elif cmd == ':send' or cmd == ':xsend':
                if cmd == ':xsend':
                    arg = arg.decode("hex")
                child.send(arg)
                time.sleep(0.2)
                shell_window = str(virtual_screen)
            elif cmd == ':cursor':
                shell_window = '%x%x' % (virtual_screen.cur_r,
                                         virtual_screen.cur_c)
            elif cmd == ':refresh':
                shell_window = str(virtual_screen)

            response = []
            response.append(shell_window)
            #response = add_cursor_blink (response, row, col)
            sent = conn.send('\n'.join(response))
            if watch_mode:
                print '\n'.join(response)
            if sent < len(response):
                print "Sent is too short. Some data was cut off."
            conn.close()
    finally:
        r.cancel()
        print "cleaning up socket"
        s.close()
        if os.path.exists("/tmp/mysock"):
            os.remove("/tmp/mysock")
        print "done!"
Beispiel #7
0
 def __init__(self):
     ConfigMixIn.__init__(self)
     self.child = None
     self.term = ANSI.ANSI()
     super(BaseConnection, self).__init__()
Beispiel #8
0
 def test_tetris (self):
     s = ANSI.ANSI (24,80)
     tetris_text = open ('tetris.data').read()
     for c in tetris_text:
         s.process (c)
     assert str(s) == tetris_target
Beispiel #9
0
class Shell(Pane.Pane):
    '''
    Shell panel, start and handles a shell (~ bash)
    '''

    nbLines = 18
    nbCols = 64
    #nbLines=10;
    #nbCols=20;
    ansi = ANSI.ANSI(nbLines - 1, nbCols - 1)
    textColor = (255, 255, 0)
    yOffset = 17
    yStep = 8

    def __init__(self, main):
        Pane.Pane.__init__(self)
        self.main = main
        self.screen = main.screen
        self.kb = VirtualKB.VirtualKB(main)

        self.font2 = pygame.font.Font(self.main.skinFolder + "/font.ttf", 8)
        self.font = pygame.font.Font(None, 12)

        self.str = ""
        self.bash = pexpect.spawn("/bin/bash")
        self.shellReader = ShellReader(self)
        self.shellReader.start()
        self.shellLines = []
        self.shellImage = pygame.image.load(self.main.skinFolder +
                                            "/shell.png")
        self.editPane = pygame.Surface((320, 240))

    def handleEvent(self, value):

        key = self.kb.handleClickKb2((value.x, value.y))
        char = key.getKey()
        if (char != None):
            # note: this will callback "updateshell" as necessary
            self.bash.send(char)
            self.main.debug("Got key: ", char)
        else:
            self.updateScreen()

    def updateScreen(self):
        self.editPane.blit(self.shellImage, (0, 0))

        if (self.shellLines != None):
            y = self.yOffset
            cpt = 0
            for line in self.shellLines:
                if (cpt > len(self.shellLines) - self.nbLines):
                    line = line.replace('\t', ' ')
                    ln = self.font2.render(line, True, self.textColor)
                    self.editPane.blit(ln, (2, y))
                    y = y + self.yStep
                cpt = cpt + 1
        self.screen.blit(self.editPane, (0, 0))
        self.kb.showKB2(self.screen, (0, 240 - 80))
        self.main.paintMenuBar()
        pygame.display.flip()

    def appendLine(self, line):
        if (line != None and len(line) > 0):
            #self.showHex(line);
            self.ansi.write(line)
            self.main.debug(self.ansi.pretty())
            self.shellLines = str(self.ansi).splitlines()
        self.updateScreen()

    def handleShutdown(self):
        self.shellReader.shutdown()
        self.bash.close()

    def showHex(self, line):
        s = ""
        for c in line:
            s += hex(ord(c)) + "(" + c + ") "
        print s