diff --git a/lib/rush/config.rb b/lib/rush/config.rb index 31da070..765e6a5 100644 --- a/lib/rush/config.rb +++ b/lib/rush/config.rb @@ -1,6 +1,7 @@ # The config class accesses files in ~/.rush to load and save user preferences. class Rush::Config DefaultPort = 7770 + DefaultSshPort = 22 attr_reader :dir diff --git a/lib/rush/ssh_tunnel.rb b/lib/rush/ssh_tunnel.rb index 5211bf6..280630d 100644 --- a/lib/rush/ssh_tunnel.rb +++ b/lib/rush/ssh_tunnel.rb @@ -2,7 +2,8 @@ # HTTP commands can be sent by Rush::Connection::Remote. class Rush::SshTunnel def initialize(real_host) - @real_host = real_host + @real_host, @real_port = real_host.split(':',2) + @real_port = @real_port || Rush::Config::DefaultSshPort end def host @@ -13,6 +14,10 @@ def port @port end + def real_port + @real_port + end + def ensure_tunnel(options={}) return if @port and tunnel_alive? @@ -81,7 +86,7 @@ class SshFailed < Exception; end class NoPortSelectedYet < Exception; end def ssh(command) - raise SshFailed unless system("ssh #{@real_host} '#{command}'") + raise SshFailed unless system("ssh -p #{real_port} #{@real_host} '#{command}'") end def make_ssh_tunnel(options={}) @@ -91,7 +96,7 @@ def make_ssh_tunnel(options={}) def ssh_tunnel_command_without_stall options = tunnel_options raise NoPortSelectedYet unless options[:local_port] - "ssh -f -L #{options[:local_port]}:127.0.0.1:#{options[:remote_port]} #{options[:ssh_host]}" + "ssh -p #{real_port} -f -L #{options[:local_port]}:127.0.0.1:#{options[:remote_port]} #{options[:ssh_host]}" end def ssh_stall_command(options={}) diff --git a/spec/ssh_tunnel_spec.rb b/spec/ssh_tunnel_spec.rb index ec54252..4f2a7c7 100644 --- a/spec/ssh_tunnel_spec.rb +++ b/spec/ssh_tunnel_spec.rb @@ -80,7 +80,7 @@ :remote_port => 456, :ssh_host => 'example.com' ) - @tunnel.ssh_tunnel_command_without_stall.should == "ssh -f -L 123:127.0.0.1:456 example.com" + @tunnel.ssh_tunnel_command_without_stall.should == "ssh -p 22 -f -L 123:127.0.0.1:456 example.com" end it "combines the tunnel command without stall and the stall command into the final command" do @@ -119,4 +119,29 @@ command.should match(/grep/) command.should match(/ssh command/) end + + describe "connecting to ssh service on alternative port" do + + before do + @tunnel_with_ssh_port = Rush::SshTunnel.new('spec.example.com:222') + @tunnel_with_ssh_port.stub!(:config).and_return(mock_config_start) + @tunnel_with_ssh_port.stub!(:display) + end + + it "recognizes alternative ssh port given in the real_host string" do + @tunnel_with_ssh_port.real_port.should == "222" + end + + it "sets port for ssh commands" do + @tunnel_with_ssh_port.should_receive(:tunnel_options).at_least(:once).and_return( + :local_port => 123, + :remote_port => 456, + :ssh_host => 'example.com' + ) + command = @tunnel_with_ssh_port.ssh_tunnel_command_without_stall + command.should match(/-p 222/) + end + + end + end