Skip to content

Instantly share code, notes, and snippets.

@ob1-sc
Last active April 14, 2022 15:33
Show Gist options
  • Save ob1-sc/571a4fdcf2b0c3626f3a468b00fef1b6 to your computer and use it in GitHub Desktop.
Save ob1-sc/571a4fdcf2b0c3626f3a468b00fef1b6 to your computer and use it in GitHub Desktop.
How to setup and use LastPass cli to retrieve secrets when scripting

Prerequisties

  1. An active LastPass account
  2. LastPass cli installed, see here

Create a password entry in LastPass with the following details:

  • name: my-super-secret
  • username: my-super-secret-username
  • password: my-super-secret-password

How to login to LastPass

lpass login <your LastPass username>

NOTE: this will prompt you to enter your LastPass password

How to retrieve the username and password of the my-super-secret entry in LastPass

MY_SUPER_SECRET_USERNAME=`lpass show --username my-super-secret`
MY_SUPER_SECRET_PASSWORD=`lpass show --password my-super-secret`

How to logout

Add the following code to the start of your script to ensure that LastPass always logs out even if the script errors:

function finish {
  lpass logout -f
}
trap finish EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment