[英]How to run the pexpect script for terminal with colored text
Pexpect works fine when I run in interactive sessions but if it has colored text then its not matching only the text,it's matching the text along with ansi colors. 当我在交互式会话中运行时,Pexpect工作正常,但如果它有彩色文本,那么它只与文本匹配,它与文本和ansi颜色匹配。 The regex for this is very complicated and big.
这方面的正则表达式非常复杂和庞大。 Can someone suggest me anything how to work with this.
有人可以建议我如何使用它。
For example: 例如:
instead of just looking for: 而不仅仅是寻找:
"opendaylight-user@root"
“opendaylight用户@根”
its looking for: 它正在寻找:
"or '\\x1b[1mlogout\\x1b[0m' to shutdown OpenDaylight.\\r\\r\\n\\r\\n\\x1b.\\r\\r\\n
“或'\\ x1b [1mlogout \\ x1b [0m'关闭OpenDaylight。\\ r \\ n \\ r \\ n \\ r \\ n \\ x1b。\\ r \\ n \\ n \\ n
\\r\\n\\x1b[36mopendaylight-user\\x1b[0m\\x1b[1m@\\x1b[0m\\x1b[34mroot\\x1b[0m>".\\ r \\ n \\ X1B [36mopendaylight用户\\ X1B [0米\\ X1B [1分@ \\ X1B [0米\\ X1B [34mroot \\ X1B [0米>”。
This is just part of the expression. 这只是表达的一部分。
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("opendaylight-user@root>")
print child.before
ex1()
Error 错误
Traceback (most recent call last):
File "ex07.py", line 11, in <module>
ex1()
File "ex07.py", line 9, in ex1
child.expect("opendaylight-user@root>")
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[1m@\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[1m@\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("opendaylight-user@root>")
I got the answer by using expect_exact() rather than expect(). 我通过使用expect_exact()而不是expect()得到了答案。 expect() matches with the regex but expect_exact matches with the strings.
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[1m@\x1b[0m\x1b[34mroot\x1b[0m>")
print child.before
ex1()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.