1

I am working on a Python script to interact with the Node. I am using pexpect module to spawn a connection and interact with the node.

I am facing issue with the expect parameters which is getting the response with some color codes. How can we match the pattern without color codes?

Here I am pasting the code snippet

import pexpect
import re
import pdb

prompt1= "Parameter\s+1\s+of\s+3,\s+restartRank\s+\(enumRef-ReqFieldReplaceableUnit.RestartRank\):\s+Enter\s+one\s+of\s+the\s+following\s+integers:\s+0:RESTART_WARM,\s+1:RESTART_COLD,\s+2:RESTART_COLDWTEST:"
expectprompts = [failPrmt, prompt1,pexpect.TIMEOUT, pexpect.EOF]
i = childconn.expect(expectprompts, timeout=tmOut)

output received from the Node

PROMPT RECEIVED FROM THE NODE ======>>>>     : <class 'pexpect.exceptions.TIMEOUT'>
Timeout / EOF Occured !!!!!!!!!!!!!!!!!!!!!!!!!!
Expect Result : ? y^M
^M
=================================================================================================================^M
Proxy  MO                                                             Action                         Nr of Params^M
=================================================================================================================^M
 4205  FieldReplaceableUnit=RRU-B1-B3-3                               restartUnit                    3^M
^M
Parameter 1 of 3, restartRank (enumRef-ReqFieldReplaceableUnit.RestartRank): ^M
   Enter one of the following integers: 0:RESTART_WARM, 1:RESTART_COLD, 2:RESTART^MT_COLDWTEST:
2021-09-02 12:04:47,097 | wessdpprodapp10 | 73543       |INFO  | Expect Result after sending ctrl-y: ? y^M
^M
=================================================================================================================^M
Proxy  MO                                                             Action                         Nr of Params^M
=================================================================================================================^M
 4205  FieldReplaceableUnit=RRU-B1-B3-3                               restartUnit                    3^M
^M
Parameter 1 of 3, restartRank (enumRef-ReqFieldReplaceableUnit.RestartRank): ^M
   Enter one of the following integers: 0:RESTART_WARM, 1:RESTART_COLD, 2:RESTART^MT_COLDWTEST:

It is not able to match the pattern as there are color codes available in between the expected output. Is there a way to match the pattern by ignoring color code in the pattern match of Pexpect Module?

In this scenario, it is not possible the receive the response and then match the pattern with other regex sources. Please guide me if there is a way to ignore ansi color codes within pexpect module.

Thanks in advance

punithavel
  • 11
  • 4
  • 1
    We can not see the color codes in your example. Can you provide a string or byte version ? – Lenormju Sep 08 '21 at 07:14
  • Does this answer your question? [ignore ANSI colors in pexpect response](https://stackoverflow.com/questions/59379174/ignore-ansi-colors-in-pexpect-response) – Lenormju Sep 08 '21 at 07:14
  • ^M Parameter 1 of 3, restartRank (enumRef-ReqFieldReplaceableUnit.RestartRank): **^M** Enter one of the following integers: 0:RESTART_WARM, 1:RESTART_COLD, 2:RESTART **^M** T_COLDWTEST: The actual text contains some control characters and color code. Please see **^M** the above text – punithavel Sep 08 '21 at 09:28
  • My requirement is not to process pexpect response, I want to match the pattern right without ansi color code and then get the response and do other activities – punithavel Sep 08 '21 at 09:35
  • 1
    There are no _color codes_ in the shown **output received from the Node**. – Armali Sep 11 '21 at 19:57

0 Answers0