The Bitwarden CLI bw
provides JSON Output.
Because I don't like to select Strings from the stdout JSON output:
jq
redirects the output into selectable snippetsfzf
selects the entry I needxclip
copies the selected entry into your clipboard (e.g. password)
I adjusted the Bash script by Christian Drumm:
pbcopy
->xclip
, becausexclip
is more common- sanity check for searchterm
- increased log messages (because of slow
bw
calls) - Reuse of BW_SESSION
- Output of entry ID for immediate acquisition in shell scripts
You may add this as ~/.local/bin/bwc
:
#!/bin/zsh
set -ef
copy_data () {
local id=$1
local login=$2
local sessionkey=$3
local totp
echo "Name: $(jq -r ".name" <<< $login), ID: $(jq -r ".id" <<< $login)"
# Copy the username to the clipboard
echo "> Copying Username"
jq -r ".login.username" <<< $login | xclip -sel clip
# Wait for user input before coping the password
echo "> Press Enter to copy password..."
read
# Copy the password to the clipboard
echo "> Copying Password"
jq -r ".login.password" <<< $login | xclip -sel clip
# Copy a TOTP Token if available
totp=$(jq -r ".login.totp" <<< $login)
if [[ $totp != "null" ]]; then
# Wait for user input before coping the totp token
echo "> Press any key to copy TOTP Token..."
read
echo "> Copying TOTP Token"
bw get totp $id --session $sessionkey | xclip -sel clip
fi
}
main() {
local searchterm=$1
local sessionkey logins login id
# Use an existing BW_SESSION or obtain a new session key
if [[ -z $BW_SESSION ]] ; then
echo "Creating new sessionkey..."
sessionkey=$(bw unlock --raw)
else
echo "Using existing sessionkey..."
sessionkey=$BW_SESSION
fi
# Search for passwords using the search term
if [[ -z $searchterm ]] ; then
echo "Searchterm can't be empty"
exit 1
fi
echo "Searching for '$searchterm'..."
logins=$(bw list items --search $searchterm --session $sessionkey)
id=$(jq -r '.[] | "\(.name)\t\(.login.username)\t\(.id)"' <<< $logins \
| fzf --reverse --with-nth=1,2 --delimiter="\t" --select-1 --exit-0 \
| awk -F"\t" '{print $3}'
)
if [[ -n $id ]]; then
login="$(jq ".[] | select(.id == \"$id\")" <<< $logins)"
copy_data $id $login $sessionkey
else
echo "No entry found..."
fi
}
main "$@"
Beware: The Bitwarden CLI is slow, so it may take some time until the output is redirected to the fzf
selection