Skip to content

Commit

Permalink
Merge pull request github#27 from github/list-all-ssh-keys
Browse files Browse the repository at this point in the history
Add a script to list all SSH keys on an appliance
  • Loading branch information
Chris Frederick committed Sep 22, 2014
2 parents 3e94295 + 29ea477 commit bbc568a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions api/ruby/enterprise/list_all_ssh_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'octokit'

# Check for environment variables
begin
access_token = ENV.fetch("GITHUB_TOKEN")
hostname = ENV.fetch("GITHUB_HOSTNAME")
rescue KeyError
puts
puts "To run this script, please set the following environment variables:"
puts "- GITHUB_TOKEN: A valid access token"
puts "- GITHUB_HOSTNAME: A valid GitHub Enterprise hostname"
exit 1
end

# Set up Octokit
Octokit.configure do |kit|
kit.api_endpoint = "#{hostname}/api/v3"
kit.access_token = access_token
kit.auto_paginate = true
end

# Get a list of all users
begin
users = Octokit.all_users
rescue
puts "\nAn error occurred."
puts "\nPlease check your hostname ('#{hostname}') and access token ('#{access_token}')."
exit 1
end

total = users.length
puts "Found #{total} users."
puts

count = 1

# Print each user's public SSH keys
users.each do |user|
# Skip organization accounts, which are included in the list of users
# but don't have any public SSH keys
if user.type == 'Organization'
puts "No keys for #{user.login} (user ##{count} of #{total})."
count += 1
next
end

keys = Octokit.user_keys(user.login)

if keys.empty?
puts "No keys for #{user.login} (user ##{count} of #{total})."
else
puts
puts "=================================================="
puts "Keys for #{user.login} (user ##{count} of #{total}):"
keys.each do |key|
puts
puts key.key
end
puts "=================================================="
puts
end

count += 1
end

0 comments on commit bbc568a

Please sign in to comment.