Class: SSHClient::Connection
- Inherits:
-
Object
- Object
- SSHClient::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/ssh_client/connection.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
- #batch_exec(&blk) ⇒ Object
- #build_config(hostname, username, password, logger: nil) ⇒ Object
- #exec(command, close: false) ⇒ Object
- #exec!(command = nil, &blk) ⇒ Object
-
#initialize(config_name = nil, hostname: nil, username: nil, password: nil, logger: nil, &blk) ⇒ Connection
constructor
A new instance of Connection.
Constructor Details
#initialize(config_name = nil, hostname: nil, username: nil, password: nil, logger: nil, &blk) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ssh_client/connection.rb', line 11 def initialize(config_name = nil, hostname: nil, username: nil, password: nil, logger: nil, &blk) build_config hostname, username, password, logger: logger if hostname @config ||= SSHClient.config config_name @transport = config.transport open batch_exec(&blk) if block_given? ObjectSpace.define_finalizer(self) { transport.close } end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/ssh_client/connection.rb', line 9 def config @config end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
9 10 11 |
# File 'lib/ssh_client/connection.rb', line 9 def transport @transport end |
Instance Method Details
#batch_exec(&blk) ⇒ Object
44 45 46 |
# File 'lib/ssh_client/connection.rb', line 44 def batch_exec(&blk) exec CommandBuilder.new(&blk).to_a.join("\n"), close: true end |
#build_config(hostname, username, password, logger: nil) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/ssh_client/connection.rb', line 22 def build_config(hostname, username, password, logger: nil) @config ||= SSHClient.configure(hostname) do |conf| conf.hostname = hostname conf.username = username conf.password = password conf.logger = logger end end |
#exec(command, close: false) ⇒ Object
31 32 33 34 |
# File 'lib/ssh_client/connection.rb', line 31 def exec(command, close: false) config.logger.info ">> #{command}" transport. command, close: close end |
#exec!(command = nil, &blk) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/ssh_client/connection.rb', line 36 def exec!(command = nil, &blk) buffer = String.new listener = add_listener(:stdout) { |data| buffer << data } block_given? ? batch_exec(&blk) : exec(command, close: true) remove_listener listener buffer end |