Allow 'vagrant ssh' to strip most non-port options.

Adds a -p flag to 'vagrant ssh' which strips out username, private key
args, allowing the local user's ssh settings to take full effect.
This commit is contained in:
Jeff Forcier 2011-05-18 18:04:54 -07:00
parent 33551eca47
commit 72b609f0a6
2 changed files with 18 additions and 5 deletions

View File

@ -14,6 +14,12 @@ module Vagrant
opts.on("-c", "--command COMMAND", "Execute an SSH command directly.") do |c| opts.on("-c", "--command COMMAND", "Execute an SSH command directly.") do |c|
options[:command] = c options[:command] = c
end end
opts.on(
"-p",
"Act like plain old 'ssh <my vm>', no extra flags"
) do |c|
options[:port_only] = c
end
end end
argv = parse_options(opts) argv = parse_options(opts)
@ -29,7 +35,7 @@ module Vagrant
if options[:command] if options[:command]
ssh_execute(vm, options[:command]) ssh_execute(vm, options[:command])
else else
ssh_connect(vm) ssh_connect(vm, {:port_only => options[:port_only]})
end end
end end
end end
@ -63,9 +69,9 @@ module Vagrant
exit exit_status exit exit_status
end end
def ssh_connect(vm) def ssh_connect(vm, opts)
@logger.debug("`exec` into ssh prompt") @logger.debug("`exec` into ssh prompt")
vm.ssh.connect vm.ssh.connect(opts)
end end
end end
end end

View File

@ -22,6 +22,9 @@ module Vagrant
# Connects to the environment's virtual machine, replacing the ruby # Connects to the environment's virtual machine, replacing the ruby
# process with an SSH process. This method optionally takes a hash # process with an SSH process. This method optionally takes a hash
# of options which override the configuration values. # of options which override the configuration values.
# One specific option, :port_only, if set to true, will cause this method
# to skip filling in some default values, such as private_key_path and
# username.
def connect(opts={}) def connect(opts={})
if Util::Platform.windows? if Util::Platform.windows?
raise Errors::SSHUnavailableWindows, :key_path => private_key_path, raise Errors::SSHUnavailableWindows, :key_path => private_key_path,
@ -30,6 +33,7 @@ module Vagrant
raise Errors::SSHUnavailable if !Kernel.system("which ssh > /dev/null 2>&1") raise Errors::SSHUnavailable if !Kernel.system("which ssh > /dev/null 2>&1")
port_only = opts[:port_only]
options = {} options = {}
options[:port] = port(opts) options[:port] = port(opts)
options[:private_key_path] = private_key_path options[:private_key_path] = private_key_path
@ -42,7 +46,8 @@ module Vagrant
# Command line options # Command line options
command_options = ["-p #{options[:port]}", "-o UserKnownHostsFile=/dev/null", command_options = ["-p #{options[:port]}", "-o UserKnownHostsFile=/dev/null",
"-o StrictHostKeyChecking=no", "-o IdentitiesOnly=yes", "-o StrictHostKeyChecking=no", "-o IdentitiesOnly=yes",
"-i #{options[:private_key_path]}", "-o LogLevel=ERROR"] "-o LogLevel=ERROR"]
command_options << "-i #{options[:private_key_path]}" unless port_only
command_options << "-o ForwardAgent=yes" if @vm.config.ssh.forward_agent command_options << "-o ForwardAgent=yes" if @vm.config.ssh.forward_agent
if @vm.config.ssh.forward_x11 if @vm.config.ssh.forward_x11
@ -51,7 +56,9 @@ module Vagrant
command_options << "-o ForwardX11Trusted=yes" command_options << "-o ForwardX11Trusted=yes"
end end
command = "ssh #{command_options.join(" ")} #{options[:username]}@#{options[:host]}".strip host_string = options[:host]
host_string = "#{options[:username]}@" + host_string unless port_only
command = "ssh #{command_options.join(" ")} #{host_string}".strip
@logger.info("Invoking SSH: #{command}") @logger.info("Invoking SSH: #{command}")
safe_exec(command) safe_exec(command)
end end