Handy 1Password CLI Wrapper Functions
November 07, 2020
I quickly got bored of having to execute the login command each time I wanted a password from 1Password CLI. These functions check for the not logged in error and ask for your password there and then.
Also the functions wrap up getting the password and copying it to the clipboard.
You will need:
- 1Password CLI
- xclip eg
apt-get install xclip
- jq eg
apt-get install jq
The following will cover 99% of my requirements from 1Password (provided I know the exact name of the password item).
Get Password
Use it like pass Google
and the password for Google will be copied to the clipboard.
pass() { | |
ITEM_NAME=$1 | |
ITEM=$(op get item $ITEM_NAME --vault Personal 2>&1) | |
if [[ $ITEM == *"You are not currently signed in"* ]]; then | |
eval $(op signin my.1password.com) | |
pass $ITEM_NAME | |
return | |
fi | |
PASSWORD=$(echo $ITEM | jq -r '.details.fields[] | select(.name == "password") | .value') | |
echo $PASSWORD | xclip -sel clip | |
echo "Password copied to the clipboard" | |
} |
Get OTP
Use it like otp Github
and the one-time password for Github will be copied to the clipboard.
otp() { | |
ITEM_NAME=$1 | |
TOTP=$(op get totp $ITEM_NAME --vault Personal 2>&1) | |
if [[ $TOTP == *"You are not currently signed in"* ]]; then | |
eval $(op signin my.1password.com) | |
totp $ITEM_NAME | |
return | |
fi | |
echo $TOTP | xclip -sel clip | |
echo "TOTP copied to the clipboard ($TOTP)" | |
} |