简体   繁体   中英

pexpect - how to pass in password using events

In this snipper I'm trying to pass the password as an event, why does the following not work?

 password = input("INPUT PASSWORD: ")
 pexpect.run("ssh -lX user@gmail.com 'ls -l'",
 events={'(?i)password':' \n'}, password)

Based on the examples provided by the documentation for pexpect, it looks like you should be passing the password on as part of the value of '(?i)password' .

from pexpect import *
run('scp foo user@example.com:.', events={'(?i)password': mypassword})

In your case this would translate to this.

pexpect.run(
    "ssh -lX eandersson@my-server.com 'ls -l'",
    events={'(?i)password': '%s\n' % password}
)

A more complete example using getpass to hide the password would look like this.

import pexpect
import getpass
password = getpass.getpass('Password: ')
print pexpect.run(
    "ssh -lX eandersson@10.0.1.1 'ls -l'",
    events={'(?i)password': '%s\n' % password}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM