Search code examples
pythonpexpect

How do I press "Enter" button after the password is passed through command-line?


I used the following code, which runs the command and prompts password. When I entered the password using sendline, for some reason the login is not happening.

How to debug this and ensure the "Enter" button is sent?

Code:

    child = pexpect.spawn('tool --server=commander.company.com login username',logfile=sys.stdout)
    child.expect('Password:')
    child.sendline('com0201')

Solution

  • sendline() will automatically send the new line character defined in os.linesep, so you shouldn't need to do anything special to send the "enter button".

    You have logging enabled; what output do you see?

    Something else to try is to child.interact() after you send the password. This will let you interact with the child process and might provide some clue if there is a problem.