【问题标题】:how to capture the python output in a shell variable from expect?如何从expect捕获shell变量中的python输出?
【发布时间】: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)

如果有其他方法可以让我知道。

【问题讨论】:

  • 我不知道如何进入expectpyexpect 有方法获取所有文本before 预期值 - 但它需要首先使用expect some_text。因为print() 在最后发送new line,所以我会使用new line 来检测打印数据的结束,然后我会使用函数来获取所有文本before 换行。
  • 对于这个脚本,我会尝试不使用expect,而是使用outcome = $(python script.py < file_with_password) 或最新的Bash outcome = $(python script.py <<< text_with_password)
  • 嗨@furas 感谢您为我指明正确的方向。我只是在期望中做了同样的事情并且它起作用了,用工作代码回答了这个问题
  • @furas 通常一个实现良好的应用程序/模块会直接从 tty 获取密码,因此标准输入重定向不起作用。这就是创建像 Expect 这样的工具的原因。

标签: python shell unix expect python-keyring


【解决方案1】:

我只是错过了一件事:

spawn python3 $pscript
expect "password:"
send "$enc_password\r"
expect "blah" --this extra expect, else the outcome will be an empty array
set outcome $expect_out(buffer) 

【讨论】:

  • 您可以将您的答案标记为已接受,几分钟后您可以投票。
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 2021-01-25
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多