1

I want to write a shell + expect script that auto-fills my passwords via the LastPass CLI utility lpass. I'm not sure how to accomplish sending the password returned by lpass to the password input in an expect script.

The expect script so far looks something like:

# The beginning isn't important
expect -exact "\r
Please enter your username and password.\r
Username:"
send -- "my-username\r"
expect -exact "my-username\r
Password:"
send -- $(lpass show --password service\ im\ connecting\ to)
expect -exact "\r
# The rest of the expect script follows

I'm not sure how the bit in the $(...) should actually be written...

1 Answer 1

2

You want to use exec function in expect script to get the same behavior as $(...) does in shell

See example below:
Let use external program 4expect.sh below that we will feed with out expect script:

#!/bin/sh
# Test program : let set filename as "4expect.sh"
# in the same directory where expect script will work

echo; read -p 'question: ' answer
echo "Got answer:>${answer}<"

And here our expect script that will wait for "question" from external program and feed it with total files in current directory (get output of external programs ls and egrep) that will be used by expect's send:

#!/usr/bin/expect -f

spawn -noecho ./4expect.sh
expect -re "question" { send -- [exec ls -la . | egrep "^total" ]\r }
interact

puts "\nDone.\n"
exit
3
  • The problem isn't getting lpass to return the password to stdout - the lpass command I gave in the example does that. It's how to capture that stdout and send it in an expect script. I haven't found that $(...) in expect scripts works as it does in regular shell scripting.
    – Josiah
    Jan 13, 2017 at 18:44
  • 1
    Ohh, man... I'm really sorry, I completely misunderstood your question. I removed my previous answer since it was irrelevant and replaced it with correct answer. Duh...
    – Alex
    Jan 14, 2017 at 4:48
  • Couple with this to handle things like your SUDO pass. stackoverflow.com/questions/681928/…. Now I can automate all my logins. fantastic!
    – Josiah
    Jan 16, 2017 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .