【发布时间】:2021-09-27 21:50:56
【问题描述】:
我想从 python 中获取 pw 值,比如
spawn python3 $pscript
expect "password:"
send "$enc_password\r"
set outcome $expect_out(buffer) --getting empty array in outcome
文件.py
import keyring
pw = keyring.get_password("system","user")
print(pw)
如果有其他方法可以让我知道。
【问题讨论】:
-
我不知道如何进入
expect但pyexpect有方法获取所有文本before预期值 - 但它需要首先使用expect some_text。因为print()在最后发送new line,所以我会使用new line来检测打印数据的结束,然后我会使用函数来获取所有文本before换行。 -
对于这个脚本,我会尝试不使用
expect,而是使用outcome = $(python script.py < file_with_password)或最新的Bashoutcome = $(python script.py <<< text_with_password) -
嗨@furas 感谢您为我指明正确的方向。我只是在期望中做了同样的事情并且它起作用了,用工作代码回答了这个问题
-
@furas 通常一个实现良好的应用程序/模块会直接从 tty 获取密码,因此标准输入重定向不起作用。这就是创建像 Expect 这样的工具的原因。
标签: python shell unix expect python-keyring