Class: SshTunnels::Tunnel
- Inherits:
-
Object
- Object
- SshTunnels::Tunnel
- Defined in:
- lib/ssh_tunnels/tunnel.rb
Overview
SSH Tunnel
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#initialize(name, user, config, gateway, passphrase) ⇒ Tunnel
constructor
A new instance of Tunnel.
- #open ⇒ Object
- #shutdown ⇒ Object
- #to_s ⇒ Object
- #toggle ⇒ Object
Constructor Details
#initialize(name, user, config, gateway, passphrase) ⇒ Tunnel
Returns a new instance of Tunnel.
8 9 10 11 12 13 14 15 16 |
# File 'lib/ssh_tunnels/tunnel.rb', line 8 def initialize(name, user, config, gateway, passphrase) @name = name @user = user @config = config @passphrase = passphrase @config = config @gateway = gateway @connection = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
6 7 8 |
# File 'lib/ssh_tunnels/tunnel.rb', line 6 def error @error end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/ssh_tunnels/tunnel.rb', line 6 def name @name end |
Instance Method Details
#active? ⇒ Boolean
37 38 39 40 41 |
# File 'lib/ssh_tunnels/tunnel.rb', line 37 def active? return false if @connection.nil? @connection.active? end |
#open ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/ssh_tunnels/tunnel.rb', line 29 def open @connection = Net::SSH::Gateway.new(@gateway.fetch('host'), @gateway.fetch('user', @user), ) @connection.open(remote_host, remote_port, local_port) rescue StandardError => e shutdown raise end |
#shutdown ⇒ Object
43 44 45 |
# File 'lib/ssh_tunnels/tunnel.rb', line 43 def shutdown @connection&.shutdown! end |
#to_s ⇒ Object
18 19 20 21 22 23 |
# File 'lib/ssh_tunnels/tunnel.rb', line 18 def to_s base = "#{local_port}:#{remote_host}:#{remote_port}" return base unless @error "#{base} (#{@error})" end |
#toggle ⇒ Object
25 26 27 |
# File 'lib/ssh_tunnels/tunnel.rb', line 25 def toggle active? ? shutdown : open end |