Skip to content

Instantly share code, notes, and snippets.

@sumskyi
Created December 27, 2010 12:01
Show Gist options
  • Select an option

  • Save sumskyi/756085 to your computer and use it in GitHub Desktop.

Select an option

Save sumskyi/756085 to your computer and use it in GitHub Desktop.
cap shell
########################################################
#
# to get this work you should install 'expect' package:
#
# sudo apt-get install expect
#
########################################################
desc "SSH Connect to servers"
task :ssh do
puts 'Aviable roles:'
role_keys = roles.keys
role_keys.each_index do |i|
print %Q{#{i+1}:#{role_keys[i]}\t=>\t}
puts roles[role_keys[i]].servers.map {|s| s.host }.inspect
end
role_id = (Capistrano::CLI.ui.ask "Select role to use[1-#{role_keys.size}]: ").to_i
raise "No such role!" unless role_id=role_keys[role_id-1]
puts "selected role: '#{role_id}'"
servers = roles[role_id].servers
server = if servers.count > 1
puts 'This role has several servers chose one:'
servers.each_index do |i|
puts "#{i+1}: #{servers[i]}"
end
servers[(Capistrano::CLI.ui.ask "Select server to connect[1-#{servers.size}]: ").to_i-1]
else
servers.first
end
if ssh_options.has_key?(:keys)
@ssh_cmd = %Q{ssh #{ssh_options[:username]}@#{server} -i #{ssh_options[:keys]}}
@real_cmd = @ssh_cmd
else
@ssh_cmd = %Q{ssh #{ssh_options[:username]}@#{server} -o "PubkeyAuthentication no"}
expect_args = %Q{spawn #{@ssh_cmd}\nexpect "*?assword:*"\nsend -- "#{ssh_options[:password]}\\r\\n"\ninteract}
require 'tempfile'
expect_command = Tempfile.new('expect_command')
expect_command.write(expect_args)
expect_command.read
@real_cmd = %Q{expect #{expect_command.path}}
end
puts "invoking cmd: #{@ssh_cmd}"
puts
system(@real_cmd)
expect_command.unlink if expect_command
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment