Class: Commutateurs::Ssh

Inherits:
Object
  • Object
show all
Defined in:
lib/commutateurs/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose) ⇒ Ssh

Returns a new instance of Ssh.



8
9
10
11
12
# File 'lib/commutateurs/ssh.rb', line 8

def initialize(verbose)
  @timeout = 10
  @verbose = verbose
  @debug = Proc.new { |line| $stderr.print(line) }
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



6
7
8
# File 'lib/commutateurs/ssh.rb', line 6

def debug
  @debug
end

#default_promptObject

Returns the value of attribute default_prompt.



6
7
8
# File 'lib/commutateurs/ssh.rb', line 6

def default_prompt
  @default_prompt
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/commutateurs/ssh.rb', line 5

def host
  @host
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/commutateurs/ssh.rb', line 5

def password
  @password
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/commutateurs/ssh.rb', line 5

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/commutateurs/ssh.rb', line 6

def timeout
  @timeout
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/commutateurs/ssh.rb', line 5

def user
  @user
end

Instance Method Details

#closeObject



53
54
55
56
57
58
59
60
# File 'lib/commutateurs/ssh.rb', line 53

def close
  Timeout::timeout(2) {
    @channel.close if @channel
    @channel = nil
    @ssh.close if @ssh
  }
rescue Timeout::Error
end

#command(cmd, options = {}) ⇒ Object



14
15
16
17
18
19
# File 'lib/commutateurs/ssh.rb', line 14

def command(cmd, options = {})
  send(cmd)
  expect(options[:prompt] || default_prompt) do |output|
    yield output if block_given?
  end
end

#connect(&block) ⇒ Object



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
# File 'lib/commutateurs/ssh.rb', line 21

def connect(&block)
  @output = []
  @channel_data = ""

  @ssh = Net::SSH.start(host, user, :port => port, :password => password, :timeout => timeout, :paranoid => Net::SSH::Verifiers::Null.new)

  @buf = ""
  @eof = false
  @channel = nil
  @ssh.open_channel do |channel|
    channel.request_pty { |ch,success| raise "failed to open pty" unless success }

    channel.send_channel_request("shell") do |ch, success|
      raise "failed to open ssh shell channel" unless success

      ch.on_data { |ch,data|
        # p data
        @debug.call(data) if @verbose
        @buf << data
      }
      ch.on_extended_data { |ch,type,data| @debug.call(data) if type == 1; @buf << data if type == 1 }
      ch.on_close { @eof = true }

      @channel = ch
      expect(default_prompt, &block)
      return
    end

  end
  @ssh.loop
end

#expect(prompt) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/commutateurs/ssh.rb', line 62

def expect(prompt)
  line = ''
  sock = @ssh.transport.socket

  while not @eof
    # p prompt
    # p line
    break if line =~ prompt and @buf == ''
    break if sock.closed?

    IO::select([sock], [sock], nil, nil)

    process_ssh

    if @buf != ""
      line += @buf.gsub(/\r\n/no, "\n")
      @buf = ''
      yield line if block_given?
    elsif @eof
      # channel has been closed
      break if line =~ prompt
      if line == ''
        line = nil
        yield nil if block_given?
      end
      break
    end
  end
  line
end

#send(line) ⇒ Object



93
94
95
# File 'lib/commutateurs/ssh.rb', line 93

def send(line)
  @channel.send_data(line + "\n")
end