2

Let me describe what I want to achieve. We are using two-factor authentication and I would like to create script which will generate TOTP token for me and it will paste it to let's say Firefox on some shortcut press. So I created a script like this

#!/bin/sh

echo $(oathtool --totp some-seed)

However if I assign shortcut for this script it's not able to print the PIN anywhere else other than the console. Ok, I know it was naive thing to try but what would be the best to achieve my goal?

3

You can use xclip.

oathtool --totp some-seed | xclip

The output of your command will be in X primary selection and you can paste it with a middle click.

You can also send it to the clipboard to paste with Ctrl+V in Firefox :

some_command | xclip -selection clipboard

Or use xdotool as explained in this question

  • Nice, however I would still prefer keyboard-only solution :) – Petr Mensik Dec 4 '14 at 16:27
  • @PetrMensik - this can be made a keyboard-only solution; e.g. you could edit your script like this: sleep 0.4; oathtool --totp some-seed | xclip -selection clipboard; xdotool key ctrl+v then assign a keyboard combination to launch the script. The sleep part is required (at least on Gnome). – don_crissti Dec 4 '14 at 18:50
1

Here is a solution that works without any pains.

Install sendkeys script from here: https://github.com/kyoto/sendkeys

For your case just do this:

oathtool --totp some-seed | xargs sendkeys.sh 'Firefox'

This will send the output of oathtool to the open Firefox window.

The script searches for windows according to its title. So, if you, let's say, want to send your script to Libreoffice Writer, just use "Writer".

1

The below xbindkeys action works without messing with the clipboard, utilising the xvkbd command.

xvkbd is a virtual (graphical) keyboard program for X Window System.

xvkbd -xsendevent -text `oathtool 00`
0

You can combine xbindkeys and xdotool to bind a key to injecting the output of a command into whatever application has the focus. Choose a key, e.g. Ctrl+Shift+F1, and put the following in your ~/.xbindkeysrc:

"xdotool type $(oathtool --totp some-seed)"
control+shift+F1

You'll need to start the program xbindkeys with your session. If you're using a desktop environment, add it to the startup programs. If you launch your session from a script such as ~/.xinitrc, add xbindkeys there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.