Stan Zeez
Stan Zeez

Reputation: 1188

Executed command and unexpected data in pexpect.before

I use pexpect for a connection to remote host through ssh and the executing of different commands for the collection of the statistic data of NetBackup (NBU) system.

import pexpect
import random
import string
import re

timeout = 600

spawn_kwargs = {'timeout': timeout, 'env': os.environ, 'ignore_sighup': False}
spawn = pexpect.spawn("ssh", **spawn_kwargs)

... # ssh authorization here

# prepare and send command
pattern = ''.join(random.sample(string.ascii_letters+string.digits, 20))
command = "'/usr/openv/volmgr/bin/vmquery' -h netbackup123 -a"
cmd = "%s %s echo %s" % (command, ";", pattern)
spawn.sendline(cmd.encode())

# expect response
patterns = [pexpectmod.EOF, pexpectmod.TIMEOUT, pattern.encode()]
expect_index = spawn.expect(patterns, timeout=timeout, searchwindowsize=1000)
if spawn.match == pexpect.TIMEOUT:
    logger.debug("got Timeout (set to %s). Got pexpect : %s", timeout, spawn.match)
    err = "Get response from session failed due timeout"
    logger.error(err)
    return -1

incoming = spawn.before.decode('utf-8', 'ignore')

# remove ANSI escape sequences
incoming = re.sub("\\x1b\[(\d+)m", "", incoming)
incoming = re.sub("\\x1b\[m", "", incoming)

print(incoming)
return 0

Usually it works well but sometimes I got bad output from the pexpect. There is the whole or a part of the executed command in the pexpect's output:

^[]0;root@netbackup123:~^G[root@netbackup123 ~]# '/usr/openv/volmgr/bin/vmquery' -h netbackup123 -a ; e ^Mcho TsiRdXP7NDMCqrwYFJ61
    ================================================================================
    media ID:              0001L1
    ...

And sometimes I got other unexpected data:

^[[A^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[C^[[K3
CLASS UNX_DSU1_MS_1streamFails71-1StreamSucceeds *NULL* 0 700000 190800 *NULL*
NAMES
...

I can not reproduce this intentionally. And such a "random" is a big problem for me :(

Environment:

Does anyone know the reason for this behavior? Thanks!

Upvotes: 1

Views: 1185

Answers (1)

satyakrish
satyakrish

Reputation: 119

This is a function that i have been using successfully for a long time to remove ansi and non-pritable chars form the pexpect response... see if it helps your case.

def rem_nonprintable_ctrl_chars(txt):
    """Remove non_printable ascii control characters """
    #Removes the ascii escape chars
    try:
        txt = re.sub(r'[^\x20-\x7E|\x09-\x0A]','', txt)
        # remove non-ascii characters
        txt = repr(txt).decode('unicode_escape').encode('ascii','ignore')[1:-1]
    except Exception as exception:
        print_exception(exception)
    return txt

Upvotes: 1

Related Questions