首页 > 解决方案 > 如何从expect捕获shell变量中的python输出?

问题描述

我想从 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)

让我知道是否有其他方法可以做到这一点。

标签: pythonshellunixexpectpython-keyring

解决方案


我只是错过了一件事:

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) 

推荐阅读