Class: Net::SSH::Proxy::Jump

Inherits:
Command
  • Object
show all
Defined in:
lib/net/ssh/proxy/jump.rb

Overview

An implementation of a jump proxy. To use it, instantiate it, then pass the instantiated object via the :proxy key to Net::SSH.start:

require 'net/ssh/proxy/jump'

proxy = Net::SSH::Proxy::Jump.new('user@proxy')
Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
...
end

ssh_config options for the jump host can be given as a Hash via options:; each is passed to the ssh command as -o Key=value:

proxy = Net::SSH::Proxy::Jump.new('user@proxy', options: { 'StrictHostKeyChecking' => 'no' })

Options that ssh_config allows more than once (+IdentityFile+, CertificateFile and SendEnv) take an Array and are passed once per element; an empty Array passes nothing:

proxy = Net::SSH::Proxy::Jump.new('user@proxy', options: { 'IdentityFile' => ['~/.ssh/jump', '~/.ssh/jump_old'] })

A nil value, alone or in an Array, raises ArgumentError, as it does for the options passed to Net::SSH.start; omit the option instead.

The options apply to the first jump host only; any further hosts in the chain are connected to by that host's own ssh.

Constant Summary

Constants inherited from Command

Command::INVALID_HOST_NAME, Command::INVALID_USER_NAME

Instance Attribute Summary collapse

Attributes inherited from Command

#command_line, #command_line_template, #timeout

Instance Method Summary collapse

Methods inherited from Command

#close_on_error

Constructor Details

#initialize(jump_proxies, options: {}) ⇒ Jump

Create a new socket factory that tunnels via multiple jump proxes as [user@]host.



41
42
43
44
45
# File 'lib/net/ssh/proxy/jump.rb', line 41

def initialize(jump_proxies, options: {})
  @jump_proxies = jump_proxies
  @options = options
  validate_options!
end

Instance Attribute Details

#jump_proxies ⇒ Object (readonly)

The jump proxies



37
38
39
# File 'lib/net/ssh/proxy/jump.rb', line 37

def jump_proxies
  @jump_proxies
end

Instance Method Details

#build_proxy_command_equivalent(connection_options = nil) ⇒ Object

We cannot build the ProxyCommand template until we know if the :config option was specified during Net::SSH.start.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/net/ssh/proxy/jump.rb', line 56

def build_proxy_command_equivalent(connection_options = nil)
  raise ArgumentError, "no jump host given" if jump_proxies.empty?

  config = connection_options && connection_options[:config]
  uri, *extra_jumps = jump_proxies.split(",", -1).map { |jump| validate_jump!(jump) }

  template = "ssh".dup
  template << " -l #{escape(uri.user)}" if uri.user
  template << " -p #{uri.port}" if uri.port
  template << " -J #{escape(extra_jumps.map { |jump| jump_spec(jump) }.join(','))}" unless extra_jumps.empty?
  template << " -F #{escape(config)}" if config != true && config
  @options.each do |key, values|
    Array(values).each { |value| template << " -o #{escape(key)}=#{escape(value)}" }
  end
  template << " -W %h:%p "
  template << escape(uri.host)

  @command_line_template = template
end

#open(host, port, connection_options = nil) ⇒ Object

Return a new socket connected to the given host and port via the jump proxy that was requested when the socket factory was instantiated.



49
50
51
52
# File 'lib/net/ssh/proxy/jump.rb', line 49

def open(host, port, connection_options = nil)
  build_proxy_command_equivalent(connection_options)
  super
end