Post

OTP Setup Guide

This guide will help you create an OTP (One-Time Password) alias that will generate an OTP, copy it to the clipboard, and then automatically paste it in the currently active window.

Prerequisites

  1. oathtool: This is a command line tool for generating OTPs.
  2. xclip: A command line interface to X selections (clipboard).
  3. xdotool: This tool lets you programmatically (or manually) simulate keyboard input and mouse activity.

Install these tools on Ubuntu or any other Debian-based distribution by running the following commands:

1
sudo apt-get install oathtool xclip xdotool

Setting up the OTP Alias

To set up the OTP alias, open your bashrc file (typically located at ~/.bashrc) with your preferred text editor. Then add the following line to the file:

1
alias otp='oathtool --base32 --totp $OTP_SECRET | xclip -sel clip; sleep 0.5; xdotool key Ctrl+v'

Replace $OTP_SECRET with your actual secret key for generating OTPs. This can be a hard-coded string or an environment variable.

This line creates an alias otp which:

  1. Generates an OTP using the oathtool.
  2. Copies the generated OTP to the clipboard with xclip.
  3. Waits for 0.5 seconds to ensure that the OTP has been copied to the clipboard.
  4. Simulates the paste operation by sending a Ctrl+V keystroke with xdotool.

After adding this line, save the file and close the text editor. Then, source your bashrc file by running the following command to make the new alias available:

1
source ~/.bashrc

Using the OTP Alias

To use the OTP alias, simply type otp in the terminal and press Enter. The alias will generate an OTP, copy it to the clipboard, and then automatically paste it into the currently active window.

Note: Be careful to ensure that the correct window or text field is focused when you run this command, as it will paste the OTP into the currently active field. Also, if your terminal doesn’t support Ctrl+V for pasting (some terminals use Ctrl+Shift+V), you’ll need to adjust the xdotool command in the alias accordingly.

This post is licensed under CC BY 4.0 by the author.

© 2023 by Rana Waqas. Proudly created with Jekyll and Chirpy.