0

I am using pexpect (pexpect-2.3-6.el6.noarch) trying to get some output via pexpect.before. However, I am getting weird characters at the very beginning:

get

ESC[?1lcurrentZone = green

What I need to get is this:

get currentZone = green

I am also using setecho(False/True), but this does not help at all.

I was searching on the internet for some tips and did not find anything which would work for me.

Here is an example of my code - the interactive program I was to interact with via pexpect is call bwcli. Within it there is a tree-like structure with various branches and possibility to traverse it via kind of 'cd' command.

f = open("new_ns_results.txt", "w")

child = pexpect.spawn('bwcli',timeout=300,maxread=10000)

child.setecho(False)

child.expect('NS_CLI> ')

child.sendline('config set yesNoPromptEnabled false')

child.expect('NS_CLI> ')

child.sendline('config set continuePromptEnabled false')

child.expect('NS_CLI> ')

child.sendline('config set scriptEchoingEnabled false')

child.expect('NS_CLI> ')

child.sendline('login admin')

child.expect('Password: ')

child.sendline('some_password')

child.expect('NS_CLI> ')

print ("Now let\'s get to work...")

child.sendline('cd /NSDiagnostic/OverloadControls/CallpUtil')

child.expect('NS_CLI/NSDiagnostic/OverloadControls/CallpUtil> ')

child.sendline('get')

child.expect('NS_CLI/NSDiagnostic/OverloadControls/CallpUtil> ')

f.write("%s" % str(child.before))

child.setecho(True)

Thanks in advance for your time. I am thankful for this, really.

ivo
  • 587
  • 8
  • 21
  • Can you reproduce the behavior with a dummy child Python script? Provide [mcve]. – jfs Jun 15 '16 at 23:50
  • See please my inserted code as an example. Is it possible that those weird characters are displayed as an output of bwcli application and actually I cannot fix it (it is not possible)? – ivo Jun 16 '16 at 13:04
  • Does bwcli produces colored or otherwise formatted text normally? It is controlled by ANSI escape sequences. If they are expected it is safe to strip them from the output. I'm not sure which part converts `b'\x1b['` into `'ESC['`. Related: [Print in terminal with colors using Python?](http://stackoverflow.com/q/287871/4279) – jfs Jun 16 '16 at 13:45

0 Answers0