Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possbile to achieve the expect script output ? #658

Open
qiulang opened this issue Sep 8, 2020 · 1 comment
Open

Is it possbile to achieve the expect script output ? #658

qiulang opened this issue Sep 8, 2020 · 1 comment

Comments

@qiulang
Copy link

qiulang commented Sep 8, 2020

I am familiar with expect script so I feel a bit odd when I first use pexpect. Take this simple script as an example,

#!/usr/bin/expect
set timeout 10
spawn npm login
expect "Username:"
send "qiulang2000\r"
expect "Password:"
send "xxxxx\r"
expect "Email:"
send "qiulang@gmail.com\r"
expect "Logged in as"
interact

When run it I will get the following output. It feels natural because that is how I run those commands

spawn npm login
Username: qiulang2000
Password:
Email: (this IS public) qiulang@gmail.com
Logged in as qiulang2000 on https://registry.npmjs.com/.

But when I use pexpect, no matter how I add print(child.after)or print(child.before) I just can't get output like expect, e.g. when I run following command

#! /usr/bin/env python3
import pexpect
child = pexpect.spawn('npm login')
child.expect('Username:')
print(child.after.decode("utf-8"))
child.sendline('qiulang2000')
child.expect('Password:')
child.sendline('xxxx')
child.expect('Email:')
child.sendline('qiulang@gmail.com')
child.expect('Logged in as')
print(child.before.decode("utf-8"))
child.interact()

I got these output, it feels unnatural because that is not what I see when I run those commands.

Username:
 (this IS public)       qiulang@gmail.com

 qiulang2000 on https://registry.npmjs.com/.

So is it it possbile to achieve the expect script output?

@EnigmaticCypher
Copy link

EnigmaticCypher commented Oct 23, 2020

@qiulang you need to combine all 3 variables from the child.

before, match, and after. This should give you the missing pieces of the output since you are not including the match variable currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants