Class: Sambot::Domain::BastionHost

Inherits:
Object
  • Object
show all
Defined in:
lib/sambot/domain/bastion_host.rb

Constant Summary collapse

ApplicationError =
Sambot::Domain::Common::ApplicationError
Parser =
Sambot::Domain::Ssh::Parser

Class Method Summary collapse

Class Method Details

.close_tunnels(tunnels) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sambot/domain/bastion_host.rb', line 31

def self.close_tunnels(tunnels)
  if tunnels
    tunnels.each do |tunnel|
      components = tunnel.split(' ')
      pid = components[1].to_i
      parse_forwards.each do |forward|
        if components[8] == forward && components[9] == '(LISTEN)' && Process.exists?(pid)
          Process.kill('INT', pid)
          UI.debug("The process #{pid}, parent of the tunnel #{forward}, has been killed.")
        end
      end
    end
  end
end

.connect(username, password) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/sambot/domain/bastion_host.rb', line 11

def self.connect(username, password)
  disconnect
  cmd = "sshpass -p #{password} ssh -N -f -l DEV\\\\#{username} bastion "
  success = system(cmd)
  sleep(3)
  unless success
    raise ApplicationError.new('Unable to open SSH tunnels to the bastion host')
  end
  self
end

.disconnectObject



27
28
29
# File 'lib/sambot/domain/bastion_host.rb', line 27

def self.disconnect()
  close_tunnels(list_tunnels)
end

.forwards(config_file = nil) ⇒ Object



22
23
24
25
# File 'lib/sambot/domain/bastion_host.rb', line 22

def self.forwards(config_file = nil)
  config = ConfigFile.new(config_file)
  []
end

.list_tunnelsObject



52
53
54
55
# File 'lib/sambot/domain/bastion_host.rb', line 52

def self.list_tunnels
  result = `lsof -i -n -P | grep -E '^ssh'`
  result.split(/\n/)
end

.parse_forwardsObject



46
47
48
49
50
# File 'lib/sambot/domain/bastion_host.rb', line 46

def self.parse_forwards
  Parser::ENTRIES[:bastion][:LocalForward].map do |entry|
    "127.0.0.1:#{entry.split(' ')[0]}"
  end
end