forked from github/platform-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request github#27 from github/list-all-ssh-keys
Add a script to list all SSH keys on an appliance
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
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
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 |