Skip to content

Instantly share code, notes, and snippets.

@Kas-tle
Last active April 30, 2023 18:33
Show Gist options
  • Save Kas-tle/35222e0e021d02cfabc63cdecdf353a4 to your computer and use it in GitHub Desktop.
Save Kas-tle/35222e0e021d02cfabc63cdecdf353a4 to your computer and use it in GitHub Desktop.
UNIX OTP Tool
[
{
"account": "My first account email@email.com",
"key": "8SDFYSDNFSDFY89SDFSDF"
},
{
"account": "My second account email@email.com",
"key": "23849HFGDKLDFIAE"
}
]
OTP_STORAGE='/full/path/to/otp.json'
PS3="Retrieve OTP: "
readarray -t options < <(jq '.[].account' ${OTP_STORAGE})
echo
select menu in "${options[@]}";
do
echo
echo "Token for ${menu}:"
secret=$(jq -r --arg TOKEN_INDEX $REPLY '.[($TOKEN_INDEX | tonumber - 1)].key' ${OTP_STORAGE})
token=$(oathtool --totp -b ${secret})
echo ${token}
# change this line below to use your system's clipboard!
# WSL: clip.exe
# MacOS: pbcopy
# Ubuntu: xclip -i
printf ${token} | pbcopy
echo
break;
done
@Kas-tle
Copy link
Author

Kas-tle commented Apr 30, 2023

Installation

MacOS

Install Homebrew if you don't already have it: https://brew.sh/

brew install oath-toolkit jq
mkdir ~/otp
curl 'https://gist.githubusercontent.com/Kas-tle/35222e0e021d02cfabc63cdecdf353a4/raw/otp.sh' -o '~/otp/otp.sh'
curl 'https://gist.githubusercontent.com/Kas-tle/35222e0e021d02cfabc63cdecdf353a4/raw/otp.json' -o '~/otp/otp.json'
chmod +x '~/otp/otp.sh'
echo 'alias otp=~/otp/otp.sh' >> ~/.zshrc
source ~/.zshrc

Ubuntu & Debian

sudo apt install oath-toolkit jq xclip
mkdir ~/otp
curl 'https://gist.githubusercontent.com/Kas-tle/35222e0e021d02cfabc63cdecdf353a4/raw/otp.sh' -o '~/otp/otp.sh'
curl 'https://gist.githubusercontent.com/Kas-tle/35222e0e021d02cfabc63cdecdf353a4/raw/otp.json' -o '~/otp/otp.json'
chmod +x '~/otp/otp.sh'
echo 'alias otp=~/otp/otp.sh' >> ~/.bashrc
source ~/.bashrc

Usage

Simply add keys and an account name for each to otp.json. You can then run otp as a command and enter a number for the one time password of the account you'd like to obtain. The OTP will be displayed and, assuming you've set it up, copied to your clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment