Class: SSHKit::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host_string_or_options_hash) ⇒ Host

Returns a new instance of Host.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sshkit/host.rb', line 23

def initialize(host_string_or_options_hash)
  @keys  = []
  @local = false

  if host_string_or_options_hash == :local
    @local = true
    @hostname = "localhost"
    @user = ENV['USER'] || ENV['LOGNAME'] || ENV['USERNAME']
  elsif !host_string_or_options_hash.is_a?(Hash)
    suitable_parsers = [
      SimpleHostParser,
      HostWithPortParser,
      HostWithUsernameAndPortParser,
      IPv6HostWithPortParser,
      HostWithUsernameParser,
    ].select do |p|
      p.suitable?(host_string_or_options_hash)
    end

    if suitable_parsers.any?
      suitable_parsers.first.tap do |parser|
        @user, @hostname, @port = parser.new(host_string_or_options_hash).attributes
      end
    else
      raise UnparsableHostStringError, "Cannot parse host string #{host_string_or_options_hash}"
    end
  else
    host_string_or_options_hash.each do |key, value|
      if self.respond_to?("#{key}=")
        send("#{key}=", value)
      else
        raise ArgumentError, "Unknown host property #{key}"
      end
    end
  end
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



9
10
11
# File 'lib/sshkit/host.rb', line 9

def hostname
  @hostname
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/sshkit/host.rb', line 9

def password
  @password
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/sshkit/host.rb', line 9

def port
  @port
end

#ssh_optionsObject

Returns the value of attribute ssh_options.



9
10
11
# File 'lib/sshkit/host.rb', line 9

def ssh_options
  @ssh_options
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/sshkit/host.rb', line 9

def user
  @user
end

Instance Method Details

#eql?(other_host) ⇒ Boolean Also known as: ==, equal?

Returns:

  • (Boolean)


72
73
74
# File 'lib/sshkit/host.rb', line 72

def eql?(other_host)
  other_host.hash == hash
end

#hashObject



64
65
66
# File 'lib/sshkit/host.rb', line 64

def hash
  user.hash ^ hostname.hash ^ port.hash
end

#key=(new_key) ⇒ Object



11
12
13
# File 'lib/sshkit/host.rb', line 11

def key=(new_key)
  @keys = [new_key]
end

#keysObject



19
20
21
# File 'lib/sshkit/host.rb', line 19

def keys
  @keys
end

#keys=(new_keys) ⇒ Object



15
16
17
# File 'lib/sshkit/host.rb', line 15

def keys=(new_keys)
  @keys = new_keys
end

#local?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sshkit/host.rb', line 60

def local?
  @local
end

#netssh_optionsObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/sshkit/host.rb', line 82

def netssh_options
  {}.tap do |sho|
    sho[:keys]          = keys     if keys.any?
    sho[:port]          = port     if port
    sho[:user]          = user     if user
    sho[:password]      = password if password
    sho[:forward_agent] = true
  end
  .merge(ssh_options || {})
end

#propertiesObject



93
94
95
# File 'lib/sshkit/host.rb', line 93

def properties
  @properties ||= OpenStruct.new
end

#to_sObject



78
79
80
# File 'lib/sshkit/host.rb', line 78

def to_s
  hostname
end

#usernameObject



68
69
70
# File 'lib/sshkit/host.rb', line 68

def username
  user
end