2017-03-17 14 views
3

Pexpect는 대화 형 세션에서 실행할 때 제대로 작동하지만 색상이있는 텍스트가 텍스트에만 일치하지 않으면 ansi 색상과 함께 텍스트가 일치합니다. 이를위한 정규식은 매우 복잡하고 크다. 누군가가 나에게이 작업 방법을 제안 할 수 있습니까? 예를 들어컬러 텍스트로 터미널에 pexpect 스크립트를 실행하는 방법

는 :

"opendaylight 사용자 @ 루트"

이 찾고 : 단지에 대한

대신 찾고

'또는'\ x1b [1mlogout \ x1b [0m '종료 OpenDaylight. \ r \ r \ n \ r \ n \ x1b. \ r \ r \ n
\ r \ n \ x1b [36mopendaylight-user \ x1b [0m \ x1b [1m @ \ x1b [0m \ x1b [34mroot \ x1b [0m> ".

이것은 표현식의 일부일뿐입니다.

import pexpect 
import os 
def ex1(): 
     os.chdir("opendaylight/distribution-karaf-0.3.4-Lithium-SR4/bin/") 
     child=pexpect.spawn("./karaf clean",cwd="/home/ubuntu/opendaylight/distribution-karaf-0.3.4-Lithium-SR4/bin/") 
     child.expect("[email protected]>") 
     print child.before 
ex1() 

오류

Traceback (most recent call last): 
    File "ex07.py", line 11, in <module> 
    ex1() 
    File "ex07.py", line 9, in ex1 
    child.expect("[email protected]>") 
    File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 321, in expect 
    timeout, searchwindowsize, async) 
    File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 345, in expect_list 
    return exp.expect_loop(timeout) 
    File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 107, in expect_loop 
    return self.timeout(e) 
    File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 70, in timeout 
    raise TIMEOUT(msg) 
    pexpect.exceptions.TIMEOUT: Timeout exceeded. 
    <pexpect.pty_spawn.spawn object at 0x7f2c5ca8ae10> 
    command: ./karaf 
    args: ['./karaf', 'clean'] 
    buffer (last 100 chars): " or '\x1b[1mlogout\x1b[0m' to shutdown  OpenDaylight.\r\r\n\r\n\x1b[36mopendaylight-user\x1b[0m\x1b[[email protected]\x1b[0m\x1b[34mroot\x1b[0m>" 
    before (last 100 chars): " or '\x1b[1mlogout\x1b[0m' to shutdown OpenDaylight.\r\r\n\r\n\x1b[36mopendaylight- user\x1b[0m\x1b[[email protected]\x1b[0m\x1b[34mroot\x1b[0m>" 
    after: <class 'pexpect.exceptions.TIMEOUT'> 
    match: None 
    match_index: None 
    exitstatus: None 
    flag_eof: False 
    pid: 20699 
    child_fd: 5 
    closed: False 
    timeout: 30 
    delimiter: <class 'pexpect.exceptions.EOF'> 
    logfile: None 
    logfile_read: None 
    logfile_send: None 
    maxread: 2000 
    searchwindowsize: None 
    delaybeforesend: 0.05 
    delayafterclose: 0.1 
    delayafterterminate: 0.1 
    searcher: searcher_re: 
    0: re.compile("[email protected]>") 
+1

변경되었습니다. 충분하지 않다면 어떤 종류의 변화가 있을지 알려주십시오 – user7369931

+0

[이스케이프 코드] (https://en.wikipedia.org/wiki/ANSI_escape_code)를 무시하고 검색해보십시오. –

답변

2

내가 expect_exact를 사용하여 대답을 얻었다() 기대보다는(). expect()는 정규식과 일치하지만 expect_exact는 문자열과 일치합니다.

import pexpect 
    import os 
    def ex1(): 
     os.chdir("opendaylight/distribution-karaf-0.3.4-Lithium-SR4/bin/") 
     child=pexpect.spawn("./karaf clean",cwd="/home/ubuntu/opendaylight/distribution-karaf-0.3.4-Lithium-SR4/bin/") 
     child.expect_exact("\x1b[36mopendaylight-user\x1b[0m\x1b[[email protected]\x1b[0m\x1b[34mroot\x1b[0m>") 
     print child.before 
    ex1()