Class: PolySSH::CommandBuilder

Inherits:
Object
  • Object
show all
Includes:
VisitorBase
Defined in:
lib/polyssh/command_builder.rb

Instance Method Summary collapse

Methods included from VisitorBase

#do_visit

Constructor Details

#initializeCommandBuilder

Returns a new instance of CommandBuilder.



6
7
8
9
10
# File 'lib/polyssh/command_builder.rb', line 6

def initialize 
  @commands = []
  @last_command = ""
  @baseport = 18000 + rand(2000)
end

Instance Method Details

#get_portObject

Detect next available port



60
61
62
63
64
65
# File 'lib/polyssh/command_builder.rb', line 60

def get_port
  @baseport += 1
  while !port_open?(@baseport) do  
    @baseport += 1
  end
end

#port_open?(port) ⇒ Boolean

Test if given port is locally used

Returns:

  • (Boolean)


68
69
70
# File 'lib/polyssh/command_builder.rb', line 68

def port_open?(port)
    !system("lsof -i:#{port}", out: '/dev/null')
end

#visit_polyssh_nodeentry(node_entry) ⇒ Object

Visit each node entry



20
21
22
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
# File 'lib/polyssh/command_builder.rb', line 20

def visit_polyssh_nodeentry node_entry
  cmd=""
  if @commands.empty? and node_entry.next.nil? then
    cmd = ("ssh " +
           "-o ForwardAgent=yes " +
           "-o UserKnownHostsFile=/dev/null " +
           "-o StrictHostKeyChecking=no " +
           "-p #{node_entry.port} " +
           "-l #{node_entry.user} " +
           "#{node_entry.host} " 
          )
    @commands << [@baseport, cmd]
  elsif node_entry.next.nil? then
    cmd = ("ssh " +
           "-o ForwardAgent=yes " +
           "-o UserKnownHostsFile=/dev/null " +
           "-o StrictHostKeyChecking=no " +
           "-p #{@baseport} " +
           "-l #{node_entry.user} " +
           "localhost " 
          )
    @commands << [@baseport, cmd]
  else
    get_port
    next_entry = node_entry.next 
    cmd = (
      "ssh " +
      "-o ForwardAgent=yes " +
      "-o UserKnownHostsFile=/dev/null " +
      "-o StrictHostKeyChecking=no " +
      "-N " +
      "-L#{@baseport}:#{next_entry.host}:#{next_entry.port} " +
      "-l #{node_entry.user} #{node_entry.host} "
    )
    @commands << [@baseport, cmd]
    next_entry.accept(self)
  end
end

#visit_polyssh_nodelist(node_list) ⇒ Object

Visit node list



13
14
15
16
17
# File 'lib/polyssh/command_builder.rb', line 13

def visit_polyssh_nodelist node_list
    if node_list.count > 0 then
      node_list.first.accept(self)
    end
end