-
Notifications
You must be signed in to change notification settings - Fork 32
/
rye
executable file
·134 lines (106 loc) · 3.48 KB
/
rye
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/ruby
# Rye -- A CLI for some handy SSH tools
#
# If your reading this via the rdocs you won't be able to see the code
# See: http://github.com/delano/rye/blob/master/bin/rye
#
# Usage: rye
#
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
#%w{net-ssh sysinfo drydock}.each { |dir| $:.unshift File.join(File.dirname(__FILE__), '..', '..', dir, 'lib') }
require 'stringio'
require 'yaml'
require 'drydock'
require 'rye'
include Drydock
global :p, :path, String, "A directory containing SSH private keys or the path to a private key"
global :V, :version, "Display version number" do
puts "Rye version: #{Rye::VERSION}"
exit 0
end
before do |obj|
# Load private keys if specified
if obj.global.path
keys = Rye.find_private_keys(obj.global.path)
Rye.add_keys(keys)
end
end
after do
end
about "Add your public keys to one or more remote machines"
usage "rye authorize [-u username] host"
option :u, :user, String, "Username"
argv :hostname
command :authorize do |obj|
raise "You must specify a host" unless obj.argv.hostname
user = Rye.sysinfo.user
opts = { :debug => nil, :auth_methods => %w(publickey hostbased) }
opts[:user] = obj.option.user if obj.option.user
opts[:user] ||= user
obj.argv.each do |hostname|
puts "Authorizing #{opts[:user]}@#{hostname}"
rbox = Rye::Box.new(hostname, opts)
puts "Added public keys for: ", rbox.authorize_keys_remote
puts "Now try: " << "ssh #{opts[:user]}@#{hostname}"
end
end
command_alias :authorize, :authorise
about "Add your public keys to your current account on this machine"
command :authorize_local do |obj|
user = Rye.sysinfo.user
puts "Authorizing #{user}@localhost"
rbox = Rye::Box.new('localhost')
puts "Added public keys for: ", rbox.authorize_keys_local
puts "Now try: " << "ssh #{user}@localhost"
end
command_alias :local_authorize, :local_authorise
about "Generate a public key from a private key"
about "Fetch the host keys for remote machines (suitable for your ~/.ssh/known_hosts file)"
usage "rye hostkey HOSTNAME [HOSTNAME2 HOSTNAME3 ...]"
argv :hostname
command :hostkeys do |obj|
raise "You must specify a host" unless obj.argv.hostname
ret = Rye.remote_host_keys(obj.argv.hostname)
STDERR.puts $/, ret.stderr, $/
puts ret.stdout
end
command_alias :hostkeys, :hostkey
about "Display your private keys"
command :keys do |obj|
Rye.keys.each do |key|
puts key.join(' ')
end
end
about "Display your public SSH keys"
usage "rye pubkeys [--pem] [KEYPATH]"
option :p, :pem, "Output in PEM format"
argv :keypath
command :pubkeys do |obj|
keys = obj.argv.empty? ? Rye.keys.collect { |k| k[2] } : obj.argv
keys.each do |path|
STDERR.puts "# Public SSH key for #{path}"
k = Rye::Key.from_file(path)
puts obj.option.pem ? k.public_key.to_pem : k.public_key.to_ssh2
end
end
command_alias :pubkeys, :pubkey
default :keys
debug :off
# We call Drydock specifically otherwise it will run at_exit. Rye also
# uses at_exit for shutting down the ssh-agent. Ruby executes at_exit
# blocks in reverse order so if Drydock is required first, it's block
# will run after Rye shuts down the ssh-agent.
begin
Drydock.run!(ARGV, STDIN) if Drydock.run? && !Drydock.has_run?
rescue Drydock::ArgError, Drydock::OptError=> ex
STDERR.puts ex.message
STDERR.puts ex.usage
rescue => ex
STDERR.puts "ERROR (#{ex.class.to_s}): #{ex.message}"
STDERR.puts ex.backtrace if Drydock.debug?
rescue Interrupt
puts "#{$/}Exiting... "
exit 1
rescue SystemExit
# Don't balk
end