如何将数据重定向到"getpass"?喜欢密码输入吗? [英] How to redirect data to a "getpass" like password input?

查看:149
本文介绍了如何将数据重定向到"getpass"?喜欢密码输入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用python脚本运行某些命令.其中一些命令要求用户输入密码,我确实尝试在其stdin中输入数据,但是它不起作用,这是两个简单的python程序代表的问题

I'm wring a python script for running some command. Some of those commands require user to input password, I did try to input data in their stdin, but it doesn't work, here is two simple python program represent the problem

input.py

import getpass

print raw_input('text1:')
print getpass.getpass('pass1:')
print getpass.getpass('pass2:')

put_data.py

put_data.py

import subprocess
import getpass

def run(cmd, input=None):
    stdin=None
    if input:
        stdin=subprocess.PIPE
    p = subprocess.Popen(cmd, shell=True, stdin=stdin)
    p.communicate(input)
    if p.returncode:
        raise Exception('Failed to run command %r' % cmd)

input ="""text1
password1
password2
"""
run('python test.py', input)

这是输出

[guest@host01 ~]# python put_data.py 
text1:text1
pass1:

它只是在pass1字段上停在那里.这是问题所在,为什么我不能将数据放到stdin来将数据提供给密码字段?如何将数据写入密码字段?

It just stop there on the pass1 field. Here is the problem, why I can't put data to stdin to feed data to the password field? How can I write data to password fields?

推荐答案

您需要 pexpect 这种情况的模块.

You need the pexpect module for such cases.

Pexpect是一个Python模块,用于 产生子应用程序和 自动控制它们. Pexpect可用于自动化 交互式应用程序(例如ssh, ftp,passwd,telnet等.

Pexpect is a Python module for spawning child applications and controlling them automatically. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc.

这篇关于如何将数据重定向到"getpass"?喜欢密码输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆