Skip to content

Instantly share code, notes, and snippets.

@seanh seanh/get-bw-session.sh
Last active Mar 28, 2020

Embed
What would you like to do?
Log in and unlock your Bitwarden vault (shell script)
#!/usr/bin/env sh
#
# Ask for your Bitwarden master password once in a nice way (shows the password as you
# type it and allows editing with backspace) and print a session key.
#
# If you save the returned session key in a BW_SESSION envvar that will "unlock"
# your Bitwarden vault for this shell env. Once BW_SESSION has been set in one shell
# env it'll also be inherited by any commands or scripts run from that shell env.
#
# Usage (sh):
#
# export BW_SESSION=`get-bw-session.sh <EMAIL>`
#
# Usage (fish):
#
# set -x BW_SESSION (get-bw-session.sh <EMAIL>)
#
# To test whether it worked run:
#
# bw unlock --check
#
# To lock the vault again manually:
#
# bw lock > /dev/null
#
# If a BW_PASSWORD envvar is set when you call this script it'll use that as the
# Bitwarden master password instead of asking your for it. So by setting BW_PASSWORD
# first you can call this script multiple times blindly without having to type
# your password multiple times:
#
# read -p "Your Bitwarden master password: " BW_PASSWORD
# get-bw-session.sh
# get-bw-session.sh
# get-bw-session.sh
#
# If your vault is already unlocked then BW_SESSION is already set and there's no need
# to call this script at all. You can use `bw unlock --check` to check whether you
# need to call this script. For example (sh):
#
# bw unlock --check > /dev/null 2>&1 || export BW_SESSION=`get-bw-session.sh <EMAIL>`
#
# Or fish:
#
# bw unlock --check > /dev/null 2>&1; or set -x BW_SESSION (get-bw-session.sh <EMAIL>)
if [ -z "${BW_PASSWORD+set}" ]
then
read -p "Your Bitwarden master password: " BW_PASSWORD
fi
bw login --check > /dev/null 2>&1
logged_in=$?
if test $logged_in -eq 0
then
bw unlock --raw "$BW_PASSWORD"
else
bw login --raw "$1" "$BW_PASSWORD"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.