Class: Train::Transports::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/train/transports/ssh.rb,
lib/train/transports/ssh_connection.rb,
lib/train/transports/cisco_ios_connection.rb

Overview

A Transport which uses the SSH protocol to execute commands and transfer files.

Author:

Defined Under Namespace

Classes: CiscoIOSConnection, Connection

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.read_options_from_ssh_config(options, option_type) ⇒ Object

Returns the ssh config option like user, port from config files Params options [Hash], option_type [String] Return String



93
94
95
96
97
# File 'lib/train/transports/ssh.rb', line 93

def self.read_options_from_ssh_config(options, option_type)
  files = options[:ssh_config_file].nil? || options[:ssh_config_file] == true ? Net::SSH::Config.default_files : options[:ssh_config_file]
  config_options = Net::SSH::Config.for(options[:host], files)
  config_options[option_type]
end

Instance Method Details

#apply_ssh_config_file(host) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/train/transports/ssh.rb', line 99

def apply_ssh_config_file(host)
  files = options[:ssh_config_file] == true ? Net::SSH::Config.default_files : options[:ssh_config_file]
  host_cfg = ssh_config_file_for_host(host, files)
  host_cfg.each do |key, value|
    # setting the key_files option to the private keys set in ssh config file
    if key == :keys && options[:key_files].nil? && !host_cfg[:keys].nil? && options[:password].nil?
      options[:key_files] = host_cfg[key]
    elsif options[key].nil?
      # Precedence is given to the option set by the user manually.
      # And only assigning value to the option from the ssh config file when it is not set by the user
      # in the option. When the option has a default value for e.g. option "keepalive_interval" has the "60" as the default
      # value, then the default value will be used even though the value for "user" is present in the ssh
      # config file. That is because the precedence is to the options set manually, and currently we don't have
      # any way to differentiate between the value set by the user or is it the default. This has a future of improvement.
      options[key] = host_cfg[key]
    end
  end
end

#connection(state = {}, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/train/transports/ssh.rb', line 77

def connection(state = {}, &block)
  apply_ssh_config_file(options[:host])
  opts = merge_options(options, state || {})
  validate_options(opts)
  conn_opts = connection_options(opts)

  if defined?(@connection) && reusable_connection?(conn_opts)
    reuse_connection(&block)
  else
    create_new_connection(conn_opts, &block)
  end
end