Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature connect to ssh on alternate ports #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rush/config.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 8 additions & 3 deletions lib/rush/ssh_tunnel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +14,10 @@ def port
@port
end

def real_port
@real_port
end

def ensure_tunnel(options={})
return if @port and tunnel_alive?

Expand Down Expand Up @@ -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={})
Expand All @@ -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={})
Expand Down
27 changes: 26 additions & 1 deletion spec/ssh_tunnel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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