Last active
February 17, 2016 17:02
-
-
Save matthutchinson/76d36b1491a1101584ab to your computer and use it in GitHub Desktop.
Send your SSH key to GitHub, with optional OTP auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install gems with; | |
# gem install io-console github_api --no-ri --no-rdoc | |
require 'github_api' | |
require 'io/console' | |
# Bypass annoying deprecation warning between the | |
# github_api gem and the faraday gem | |
Faraday::Builder = Faraday::RackBuilder | |
print "Github Username: " | |
user_name = gets.strip | |
print "Github Password (nothing will be displayed): " | |
password = STDIN.noecho(&:gets).strip | |
print "\n2 Factor Auth (optional one time password): " | |
otp = gets.strip | |
github = Github.new do |config| | |
config.basic_auth = "#{user_name}:#{password}" | |
unless otp.empty? | |
config.connection_options = {headers: {"X-GitHub-OTP" => otp}} | |
end | |
end | |
begin | |
github.users.keys.create("title" => "FirehoseVagrant", | |
"key"=> File.open("/home/vagrant/.ssh/id_rsa.pub").read) | |
puts "\nok!" | |
rescue Github::Error::UnprocessableEntity | |
puts "\nAdding SSH key failed, is it possible you've already added it? check here: https://github.com/settings/ssh" | |
rescue Github::Error::Unauthorized | |
puts "\nAuthentication Failed, check your username & password (or one-time-password)" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment