-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-config-instance
executable file
·200 lines (178 loc) · 5.8 KB
/
ssh-config-instance
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env ruby
#
# Copyright 2013-2016 Victor Penso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 'ostruct'
require 'getoptlong'
require 'logger'
class System
def self.exec(command)
command + ' 2>&1'
$logger.debug("Exec [#{command}]")
# Execute command as subprocess and return the exit code
pipe = IO.popen(command)
# Get the process ID from the child
pid = pipe.pid
# Read the output from the stream
output = pipe.read
# Wait for successful return and pass back the code to the caller
Process.wait(pid)
state=$?
$logger.debug "Returned with #{state}"
if state == 0
return output
else
$logger.warn "Failed to execute [#{command}]"
return nil
end
end
end
exec_name = File.split(__FILE__)[-1]
HELP = <<EOF
#{exec_name} [<options>] [<ip>] [<path>]
<ip> IP-address or hostname of the remote instance.
(defaults to 10.1.1.28).
<path> Target directroy to write the configuration file
(defaults to ssh_config in the working directory).
<options>
-d,--debug Show stacktraces in case of errors
-h,--help Show this help information
-i,--identity-file [<path>] Identity key to use for the connection.
-k,--kerberos Enable GSSAPI Kerberos authentication.
-O,--overwrite Always overwrite existing configuration files.
-u,--user <name> User name for the connection.
--version Program version number.
EOF
begin
options = OpenStruct.new
options.path = ENV['PWD']
options.debug = false
options.user = 'devops'
options.id_file = String.new
options.ip = '10.1.1.30'
options.overwrite = false
options.kerberos = false
$logger = Logger.new($stderr)
# Adjust the time format used for the logger
$logger.datetime_format = "%Y-%m-%dT%H:%M:%S"
$logger.formatter = proc do |severity, datetime, progname, message|
"[#{datetime.strftime($logger.datetime_format)}] #{severity} -- #{exec_name} -- #{message}\n"
end
$logger.level = Logger::FATAL
# Read all shell environment variables
if ENV.has_key? 'VM_TOOLS_DEBUG'
options.debug = true
$logger.level = Logger::DEBUG
end
GetoptLong.new(
['--debug','-d',GetoptLong::NO_ARGUMENT],
['--help','-h',GetoptLong::NO_ARGUMENT],
['--identity-file','-i',GetoptLong::REQUIRED_ARGUMENT],
['--kerberos','-k',GetoptLong::NO_ARGUMENT],
['--log-level','-L',GetoptLong::REQUIRED_ARGUMENT],
['--overwrite','-O',GetoptLong::NO_ARGUMENT],
['--user','-u',GetoptLong::REQUIRED_ARGUMENT],
['--version',GetoptLong::NO_ARGUMENT]
).each do |opt,arg|
case opt
when '--debug'
options.debug = true
$logger.level = Logger::DEBUG
when '--help'
$stdout.puts HELP
exit 0
when '--identity-file'
options.id_file = File.expand_path(arg)
when '--kerberos'
options.kerberos = true
when '--log-level'
$logger.level = case arg
when 'warn'
Logger::WARN
when 'debug'
Logger::DEBUG
when 'fatal'
Logger::FATAL
else
Logger::INFO
end
when '--overwrite'
options.overwrite = true
when '--user'
options.user = arg
when '--version'
$stdout.puts 1.1
exit 0
end
end
hostname = ARGV[0] || options.ip
if ARGV.length > 1
options.path = ARGV[1]
raise("#{options.path} not existing ") unless File.directory? options.path
end
config_file = "#{options.path}/ssh_config"
if File.exist? config_file and not options.overwrite
$stdout.print "Overwrite #{config_file}? (Enter/Y/y to continue) "
answer = $stdin.gets.chomp
exit 0 unless answer.empty? or "Y" == answer.upcase
end
config = Array.new
config << "User #{options.user}"
config << "HostName #{hostname}"
config << "UserKnownHostsFile /dev/null"
config << "StrictHostKeyChecking no"
config << "GSSAPIAuthentication yes" if options.kerberos
##
# If the user dose not provide an SSH key-pair
if options.id_file.empty?
# directory to store the SSH key
path = "#{ENV['PWD']}/keys"
System::exec('mkdir keys') unless File.directory?(path)
# SSH key name
key = "#{path}/id_rsa"
unless File.exist?(key)
System::exec("ssh-keygen -q -t rsa -b 2048 -N '' -f keys/id_rsa")
$stdout.puts("Password-less SSH key-pair create in #{path}")
end
# append to the SSH configuration
config << "IdentityFile #{key}"
##
# The user provides an SSH key
#
else
if File.exist? options.id_file
config << "IdentityFile #{options.id_file}"
else
raise("SSH identity file missing #{options.id_file}")
end
end
##
# Write the SSH configuration file
#
System::exec(%Q[/bin/echo -e "Host instance\\n #{config.join('\\n ')}" > #{config_file} ])
$stdout.puts "SSH configuration: #{config_file}"
rescue => exc
$stderr.puts "ERROR: #{exc.message}"
$stderr.puts " use -h for detailed instructions"
if options.debug
$stderr.puts '-- Stack Trace --'
$stderr.puts exc.backtrace
else
$stderr.puts 'You may want run this in debug mode with \'-d\''
end
exit 1
end
exit 0