Why captured match is displayed as part of pexpect ".after" ?

Viet Nguyen vhnguyenn at yahoo.com
Sun Sep 14 07:44:16 CEST 2014


If any is familiar with pexpect, please help to point out why my script seems to fail to capture the desired text.

Here, I want to log into a server  Once I see "Username: ", I will type in my userid "admin".

The problem here is I have a list of keywords for pexpect to match before returning the matched item for me.  The list consists of following list items:

expects = ['yes', '.*Username: ', '.*login: |.*Login: ', 'SY-FAR-1>', 'SY-FAR-1#', 'Password: |password: ', '.*Press RETURN to get started']

Here, it seems my script found "Username: " but it got displayed as PART OF _pexpect_session.after  (and this is what I don't understand).  The ".before" or ".after" data should consist of match before and after the desired keyword only.  

============= Manual Execution =======================
Connected to
Escape character is '^]'.

User Access Verification

Username: admin

=============== Python Script Snippet =======================
        expects = ['yes', '.*Username: ', '.*login: |.*Login: ', 'SY-FAR-1>', 'SY-FAR-1#', 'Password: |password: ', '.*Press RETURN to get started']

        i = -1   # reset its value
	print "*step 1*before match, i = %s" % i

	self._pexpect_session.timeout = 80
        cmd = "admin"
        self._log("Executing CLI: '{0}'".format(cmd))
        i = self._pexpect_session.expect(expects)
	print "*step 2* found index i = %s" % i 

 	print "*step 3* after match, exec_cmd expects (%s) =" % i, expects
        print "*step 4* exec_cmd match before: '%s'" % self._pexpect_session.before
        print "*step 5* exec_cmd match after: '%s'" % self._pexpect_session.after 
=============== Actual Script Output ======================

*step1* before match, i = -1
Executing CLI: 'admin'
*step2* found index i = 1
*step3* after match, exec_cmd expects (1) = ['yes', '.*Username: ', '.*login: |.*Login: ', 'SY-FAR-1>', 'SY-FAR-1#', 'Password: |password: ', '.*Press RETURN to get started']

*step4* exec_cmd match before: ''
*step5* exec_cmd match after: '
Username: '

Appreciate any input or explanation.


More information about the Python-list mailing list