Class: Net::SSH::Multiplex::ControlPath

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ssh/multiplex/control_path.rb

Overview

Expands a ControlPath the way OpenSSH's ssh does: a leading ~ first, then $ENV and the % tokens below in a single pass (ssh.c, DEFAULT_CLIENT_PERCENT_EXPAND_ARGS in sshconnect.h, misc.c percent_dollar_expand).

%%  a literal %
%C  SHA1 hex of %l%h%p%r%j
%d  local home directory
%h  remote host name, after HostName
%i  local uid
%j  host of the last ProxyJump hop, or empty
%k  the host key alias, or the host name as given
%L  local host name up to the first dot
%l  local host name
%n  the host name as given
%p  remote port
%r  remote user
%u  local user

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host_arg:, host:, port:, remote_user:, jump_host: '', host_key_alias: nil) ⇒ ControlPath

host_arg is the host as given, host the one connected to after HostName.



30
31
32
33
34
35
36
37
38
# File 'lib/net/ssh/multiplex/control_path.rb', line 30

def initialize(host_arg:, host:, port:, remote_user:, jump_host: '', host_key_alias: nil) # rubocop:disable Metrics/ParameterLists
  @host_arg = host_arg.to_s
  # ssh lowercases host names, but not addresses, before expanding.
  @host = address?(host.to_s) ? host.to_s : host.to_s.downcase
  @port = port.to_s
  @remote_user = remote_user.to_s
  @jump_host = jump_host.to_s
  @host_key_alias = host_key_alias
end

Class Method Details

.jump_host(proxy_jump) ⇒ Object

The host ssh uses for %j: the last hop of a ProxyJump list, without user or port (readconf.c, parse_jump).



70
71
72
73
74
75
76
77
78
# File 'lib/net/ssh/multiplex/control_path.rb', line 70

def self.jump_host(proxy_jump)
  last = proxy_jump.to_s.split(',').last.to_s.strip.delete_prefix('ssh://')
  last = last.split('@').last.to_s
  if last.start_with?('[')
    last[1...last.index(']').to_i]
  else
    last.split(':').first.to_s
  end
end

Instance Method Details

#expand(path) ⇒ Object

Raises ArgumentError for an unknown % token, an unset $ENV variable or an unknown ~user, which make ssh refuse the ControlPath too.



43
44
45
# File 'lib/net/ssh/multiplex/control_path.rb', line 43

def expand(path)
  expand_tokens(expand_tilde(path))
end

#tokens ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/net/ssh/multiplex/control_path.rb', line 47

def tokens
  @tokens ||= begin
    pw = Etc.getpwuid(Process.uid)
    local_host = Socket.gethostname
    {
      'C' => Digest::SHA1.hexdigest("#{local_host}#{@host}#{@port}#{@remote_user}#{@jump_host}"),
      'd' => pw.dir,
      'h' => @host,
      'i' => Process.uid.to_s,
      'j' => @jump_host,
      'k' => @host_key_alias || @host_arg,
      'L' => local_host.split('.', 2).first,
      'l' => local_host,
      'n' => @host_arg,
      'p' => @port,
      'r' => @remote_user,
      'u' => pw.name
    }
  end
end