简体   繁体   English

pexpect-如何使用事件传递密码

[英]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' . 根据pexpect 文档提供的示例,您似乎应该将密码作为'(?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. 使用getpass隐藏密码的更完整示例如下所示。

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}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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