0

I want to get the pw value from python, something like

spawn python3 $pscript
expect "password:"
send "$enc_password\r"
set outcome $expect_out(buffer) --getting empty array in outcome

file.py

import keyring
pw = keyring.get_password("system","user")
print(pw)

let me know if there is any other way to do this.

  • I don't know how to get in `expect` but `pyexpect` has method to get all text `before` expected value - but it needs to use `expect some_text` first. Because `print()` sends `new line` at the end so I would use `new line` to detect end of printed data and later I would use function to get all text `before` new line. – furas Jul 20 '21 at 11:30
  • for this script I would try to do it without `expect` but something like `outcome = $(python script.py < file_with_password)` or in the newest `Bash` `outcome = $(python script.py <<< text_with_password)` – furas Jul 20 '21 at 11:35
  • Hi @furas Thank you for pointing me in the right direction. I just did the same thing in expect and it worked, answering the question with working code – deba prasad nayak Jul 20 '21 at 13:16
  • @furas usually a well implemented app/module would get password directly from the tty so stdin redirection would not work. that's why tools like Expect are created. – sexpect - Expect for Shells Jul 21 '21 at 03:15

1 Answers1

1

I just missed one thing:

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)