Instantly share code, notes, and snippets.

Embed
What would you like to do?
Command-line Google Authenticator (TOTP)
Sample Account 1 ABCDEF0123456789
Sample Account 2 0123456789ABCDEF
#!/usr/bin/env ruby
require 'rubygems'
require 'rotp'
printf "%-30s %3s (%02ds) %4s\n", 'Account', 'Now', (30 - (Time::now.utc.to_i % 30)), 'Next'
puts '-' * 47
File::read(File.expand_path('~/.google-authenticator-accounts')).split("\n").reject{|l| l.strip == ''}.each do |account|
if account =~ /^(.+) ([\w\d]+)$/
totp = ROTP::TOTP.new($2)
printf "%-30s %06s %06s\n", $1, totp.at(Time::now.utc), totp.at(Time::now.utc + 30)
end
end
@Dan-Q

This comment has been minimized.

Copy link
Owner

Dan-Q commented Dec 10, 2018

How to use:

Create ~/.google-authenticator-accounts containing account names and secret keys; one account per line, with a space separating the account from the (alphanumeric) key. Protect this file.

Run ./google-authenticator.rb to generate "now" and "next" TOTP codes for the entire list.

Further reading: https://danq.me/2018/12/10/second-factor-safety-net/

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