diff --git a/bin/vagrant-ssh-config b/bin/vagrant-ssh-config new file mode 100755 index 000000000..f754a1c2d --- /dev/null +++ b/bin/vagrant-ssh-config @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +begin + require File.expand_path('../../.bundle/environment', __FILE__) +rescue LoadError + # Fallback on rubygems + require "rubygems" +end + +require 'git-style-binary/command' + +# Get library +libdir = File.join(File.dirname(__FILE__), '..', 'lib') +require File.expand_path('vagrant', libdir) + +GitStyleBinary.command do + short_desc "outputs .ssh/config valid syntax for connecting to this environment via ssh" + banner <<-EOS +Usage: #{command.full_name} #{all_options_string} + +Outputs a .ssh/config valid entry for connecting (via SCP, git, etc.) +to the current environment. + +EOS + + run do |command| + Vagrant::Commands.execute(:ssh_config) + end +end diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 2a358c3eb..8d9cf6f93 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -61,6 +61,18 @@ module Vagrant env.ssh.connect end + # Outputs a valid entry for .ssh/config which can be used to connect + # to this environment. + def ssh_config + env.require_persisted_vm + puts TemplateRenderer.render("ssh_config", { + :host_key => "vagrant", + :ssh_user => env.config.ssh.username, + :ssh_port => env.ssh.port, + :private_key_path => env.config.ssh.private_key_path + }) + end + # Halts a running vagrant instance. This forcibly halts the instance; # it is the equivalent of pulling the power on a machine. The instance # can be restarted again with {up}. diff --git a/templates/ssh_config.erb b/templates/ssh_config.erb new file mode 100644 index 000000000..f5131f467 --- /dev/null +++ b/templates/ssh_config.erb @@ -0,0 +1,7 @@ +Host <%= host_key %> + HostName localhost + User <%= ssh_user %> + Port <%= ssh_port %> + UserKnownHostsFile /dev/null + StrictHostKeyChecking no + IdentityFile <%= private_key_path %> diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 7567a8915..0b832a0c1 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -108,6 +108,27 @@ class CommandsTest < Test::Unit::TestCase end end + context "ssh-config" do + setup do + @ssh = mock("ssh") + @ssh.stubs(:port).returns(2197) + @env.stubs(:ssh).returns(@ssh) + end + + should "output rendered template" do + result = mock("result") + Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", { + :host_key => "vagrant", + :ssh_user => @env.config.ssh.username, + :ssh_port => @env.ssh.port, + :private_key_path => @env.config.ssh.private_key_path + }).returns(result) + + @commands.expects(:puts).with(result).once + @commands.ssh_config + end + end + context "suspend" do setup do @persisted_vm.stubs(:suspend)