B, an ugly Bitwarden CLI


Yes, yes, yes, I know… I don’t like graphical interfaces so sometimes I like to make some TUI tools, and sometimes they can be ugly too…

I was tired of wasting time clicking each time on the web extension or on the client, of having to systematically enter my login details, of not having a quick access directly accessible in shortcuts, etc.

So, living in a terminal 90% of the time, I thought I’d do a quick script to put me out of my misery.

I know, judge me, please yourself :

1
2
3
4
# b
# […]
bw list items --search "${1}" --session "${BW_SESSION}" | jq -j '.[] | [.name, .login.password]' | tr -d '\n["'| tr ']' '\n'| tr ',' ':' | sed 's/^\ \ //g' | "${FZF}" --layout=reverse | tail -1 | tr -d '"' | cut -d ':' -f 2 | sed 's/^\ \ //g'| cat | xclip -se c > /dev/null 2> /dev/null
# […]

I needed Bitwarden CLI tool bw, a way to copy in clipboard password xclip, some json parsing with jq and fzf (and fzf-tmux).

So dependencies are :

  • bw (Bitwarden cli)
  • xclip
  • jq
  • fzf
  • fzf-tmux

To install this quick and dirty script, you need to create a Bitwarden token, check documentation here. Then set variables in b script.

1
2
3
4
# Fill these vars
export BW_CLIENTID=''
export BW_CLIENTSECRET=''
export BW_PASSWORD=''

Finally move b file to /usr/local/bin.

1
sudo mv b /usr/local/bin 

You can then :

  • Search for a password
1
2
# ex, search github password
b github
  • Sync vault (download new password from Bitwarden/Vaultwarden server):
1
b --sync

And probably a lot more to come as I could be a lazy man.

Full script :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash

export BW_CLIENTID=''
export BW_CLIENTSECRET=''
export BW_PASSWORD=''

export LOCK="$HOME/.bw.lock"

# is unlock ?
if test -f "${LOCK}"; then
  BW_SESSION=$(bw unlock --passwordenv BW_PASSWORD | grep export | cut -d '"' -f 2 | tr -d '"' && bw sync)
else
  bw login --apikey && touch "${LOCK}"
fi

# is inside tmux ?
if [ -z "${TMUX}" ] ;  then
  FZF="fzf"
else 
  FZF="fzf-tmux"
fi

if [ "${1}" = "--sync" ]; then
  bw sync
  exit
fi

bw list items --search "${1}" --session "${BW_SESSION}" | jq -j '.[] | [.name, .login.password]' | tr -d '\n["'| tr ']' '\n'| tr ',' ':' | sed 's/^\ \ //g' | "${FZF}" --layout=reverse | tail -1 | tr -d '"' | cut -d ':' -f 2 | sed 's/^\ \ //g'| cat | xclip -se c > /dev/null 2> /dev/null

Sources are here.