2FA Bash script
I would like to share a simple #2FA #Bash script with you, you. it is a #wrapper script around the oathtool
program. You only have to then memorize simple names, such as 2fa.sh itchio
or so.
Some examples:
$ 2fa.sh itchio
$ 2fa.sh friendica
So you only have to memorize itchio
instead of your secret key.
Here is the script, public domain:
#!/bin/bash
# Helper script to easier store your secret keys for 2FA, needs
# oathtool being installed
#
# Author: Roland Haeder / Fediverse: roland@f.haeder.net
# Variables:
OATHTOOL_BIN=$(which oathtool)
BASE_PATH="${HOME}/.2fa"
SECRET_FILE="${BASE_PATH}/.secrets"
SECRET_KEY=""
# Check conditions, requires one parameter, oathtool and create base path
if [ -z "$1" ]
then
echo "Usage $0 <service>"
exit 1
elif [ -z "${OATHTOOL_BIN}" ]
then
echo "Program 'oathtool' is not in your path or installed."
exit 1
elif [ ! -d "${BASE_PATH}" ]
then
mkdir "${BASE_PATH}"
chmod 0700 "${BASE_PATH}"
fi
if [ ! -f "${SECRET_FILE}" ]
then
touch "${SECRET_FILE}"
chmod 0600 "${SECRET_FILE}"
fi
echo "Looking up secret key for service '$1' ..."
SECRET_KEY=$(grep "$1" ${SECRET_FILE} | cut -d " " -f 2)
if [ -z "${SECRET_KEY}" ]
then
echo "Secret key for service '$1' not found."
while [ -z "${SECRET_KEY}" ]
do
read -s -p "Please enter it: " SECRET_KEY
done
echo "Adding secret key ..."
echo "$1 ${SECRET_KEY}" >> "${SECRET_FILE}"
fi
echo "TOTP code is:"
${OATHTOOL_BIN} -b --totp "${SECRET_KEY}"
Have fun!
Roland Häder🇩🇪
in reply to Roland Häder🇩🇪 • •Roland Häder🇩🇪
in reply to Roland Häder🇩🇪 • •