Le Baron de Charlus


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 :

# 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 :

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

# Fill these vars
export BW_CLIENTID=''
export BW_CLIENTSECRET=''
export BW_PASSWORD=''

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

sudo mv b /usr/local/bin 

You can then :

# ex, search github password
b github
b --sync

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

Full script :

#!/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.

#Bitwarden #Cli #Shell