Skip to content

Instantly share code, notes, and snippets.

@Dunedan
Last active April 10, 2017 17:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dunedan/14cfb8b5243f374fe340 to your computer and use it in GitHub Desktop.
Save Dunedan/14cfb8b5243f374fe340 to your computer and use it in GitHub Desktop.
OTP token generation script
#!/bin/sh
# Reads OTP secrets belonging to an readable name from a file and generates OTP
# tokens. The secrets should be in a file, separated by a colon.
# E.g. test:secretkey
#
# Works really nicely together with an alias:
# alias otp='~/otp.sh'
#
# Licensed under CC0 <https://creativecommons.org/publicdomain/zero/1.0/>
SECRETS_FILE=~/.otp-secrets
if [ ! $(command -v oathtool) ]; then
echo >&2 "Can't find oathtool. Please install it."
exit 1
fi
if [ ! -r "$SECRETS_FILE" ]; then
echo >&2 "Can't read your otp secrets from $SECRETS_FILE"
exit 1
fi
if [ "$1" = "--avail" ]; then
cat "$SECRETS_FILE" | cut -d":" -f 1
exit 0
elif [ -z "$1" -o "$1" = "--help" ]; then
echo "Usage: $0 entry"
echo " $0 --avail"
exit 0
fi
SECRET_KEY=$(grep "^$1 *:" "$SECRETS_FILE" | cut -d":" -f 2)
if [ "$SECRET_KEY" != "" ]; then
oathtool --totp -b $SECRET_KEY
else
echo >&2 "Unknown key \"$1\""
exit 1
fi
@Dunedan
Copy link
Author

Dunedan commented Sep 15, 2016

With the help of xclip it's also easy to get the token directly into the clipboard for pasting:

diff --git a/otp.sh b/otp.sh
index f9d4944..531b40c 100644
--- a/otp.sh
+++ b/otp.sh
@@ -31,7 +31,7 @@ fi
 SECRET_KEY=$(grep "^$1 *:" "$SECRETS_FILE" | cut -d":" -f 2)

 if [ "$SECRET_KEY" != "" ]; then
-    oathtool --totp -b $SECRET_KEY
+    oathtool --totp -b $SECRET_KEY | xclip -selection clipboard
 else
     echo >&2 "Unknown key \"$1\""
     exit 1

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