# File lib/rudy/cli/machines.rb, line 201
201:       def ssh
202:         
203:         
204:         # TODO: Give this method a good look over
205:         pkey = current_user_keypairpath
206:         unless pkey
207:           li "No private key configured for #{current_machine_user} in #{current_machine_group}"
208:         end
209:         
210:         # Options to be sent to Rye::Box
211:         rye_opts = { :user => current_machine_user, :keys => [], :debug => nil, :info => STDOUT }
212:         if File.exists? pkey 
213:           #raise "Cannot find file #{pkey}" unless File.exists?(pkey)
214:           if Rudy.sysinfo.os != :windows && File.stat(pkey).mode != 33152
215:             raise InsecureKeyPermissions, pkey 
216:           end
217:           rye_opts[:keys] << pkey 
218:         end
219:         
220:         local_keys = Rye.keys
221:         rye_opts[:keys] += local_keys if local_keys.is_a?(Array)
222:         
223:         li "# SSH OPTS", rye_opts.to_yaml if @@global.verbose > 3
224:         
225:         # The user specified a command to run. We won't create an interactive
226:         # session so we need to prepare the command and its arguments
227:         if @argv.first
228:           command, command_args = @argv.shift, @argv || []
229:           Rudy::Huxtable.ld "COMMAND: #{command} #{command_args.join(' ')}" if @@global.verbose > 1
230: 
231:         # otherwise, we'll open an ssh session or print command
232:         else
233:           command, command_args = :interactive_ssh, @option.print.nil?
234:         end
235:         
236:         if command == :interactive_ssh && @global.parallel
237:           raise "Cannot run interactive sessions in parallel"
238:         end
239:         
240:         checked = false
241:         lt = get_machines
242:         
243:         rset = Rye::Set.new(current_machine_group, :parallel => @global.parallel)
244:         lt.each do |machine|
245:           if Rudy::Machine === machine 
246:             machine.refresh! # make sure we have the latest DNS info 
247:             rbox = Rye::Box.new(machine.dns_public, rye_opts)
248:             rbox.nickname = machine.name
249:             instid = machine.instid
250:           else
251:             rbox = Rye::Box.new(machine, rye_opts)
252:             rbox.nickname = machine
253:             instid = ''
254:           end
255:           
256:           if command == :interactive_ssh
257:             # Print header
258:             if @@global.quiet
259:               print "You are #{rye_opts[:user].to_s.bright}. " if !checked # only the 1st
260:             else
261:               li machine_separator(rbox.nickname, instid)
262:               li "Connecting #{rye_opts[:user].to_s.bright}@#{rbox.host} "
263:               li
264:             end
265:           else
266:             unless @global.parallel
267:               rbox.pre_command_hook do |cmd,user,host,nickname|
268:                 print_command user, nickname, cmd
269:               end
270:             end
271:             rbox.post_command_hook do |ret|
272:               print_response ret
273:             end
274:           end
275: 
276:           # Make sure we want to run this command on all instances
277:           if !checked && command != :interactive_ssh 
278:             execute_check(:low) if current_machine_user == "root"
279:             checked = true
280:           end
281:           
282:           # Open the connection and run the command          
283:           if command == :interactive_ssh
284:             rbox.send(command, command_args) 
285:           else
286:             rset.add_box rbox
287:           end
288:         end
289:         
290:         rset.send(command, command_args) unless command == :interactive_ssh
291:         
292:       end