Module: Conn::DSL

Included in:
Conn
Defined in:
lib/conn/dsl.rb

Instance Method Summary collapse

Instance Method Details

#ssh(hostname, &blk) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/conn/dsl.rb', line 10

def ssh(hostname, &blk)
  queue = Queue.new
  cmd_loop = Thread.new do
    blk.call(queue)
    queue.respond_to?(:close) ? queue.close : (queue << false)
  end
  Net::SSH.start(*to_ssh_config(hostname)) do |ssh|
    ssh.loop do
      msg = queue.pop
      if msg
        input(msg)
        ssh.exec!(msg) do |chan, stream, data|
          if stream == :stdout
            stdout(data)
          else
            stderr(data)
          end
        end
        true
      else
        false
      end
    end
  end
  cmd_loop.join if cmd_loop.alive?
end

#ssh_pty(hostname, &blk) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/conn/dsl.rb', line 70

def ssh_pty(hostname, &blk)
  queue = Queue.new
  cmd_loop = Thread.new do
    blk.call(queue)
    queue.respond_to?(:close) ? queue.close : (queue << false)
  end
  Net::SSH.start(*to_ssh_config(hostname)) do |ssh|
    ssh.loop do
      msg = queue.pop
      if msg
        input(msg)
        ssh.exec!(msg) do |chan, stream, data|
          if stream == :stdout
            stdout(data)
          else
            stderr(data)
          end
        end
        true
      else
        false
      end
    end
  end
  cmd_loop.join if cmd_loop.alive?
end

#ssh_try(hostname) ⇒ Object



43
44
45
46
47
# File 'lib/conn/dsl.rb', line 43

def ssh_try(hostname)
  ssh_try!(hostname)
rescue => e
  false
end

#ssh_try!(hostname) ⇒ Object



37
38
39
40
41
# File 'lib/conn/dsl.rb', line 37

def ssh_try!(hostname)
  Net::SSH.start(*to_ssh_config(hostname, timeout: 3)) do |ssh|
    ssh.exec! 'uptime'
  end
end