Class: Worker

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

Instance Method Summary collapse

Constructor Details

#initialize(hostname: nil, username: nil, password: nil, pkey_password: nil, sudo_password: nil, command: nil, block: nil, header_max_length: nil, debug: nil) ⇒ Worker

Returns a new instance of Worker.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/worker.rb', line 2

def initialize(hostname: nil, username: nil, password: nil, pkey_password: nil, sudo_password: nil, command: nil, block: nil, header_max_length: nil, debug: nil)
  @hostname       = hostname
  @username       = username
  @password       = password
  @pkey_password  = pkey_password
  @sudo_password  = sudo_password
  @command        = command
  @block          = block

  @header_begin   = hostname
  @header_padding = header_max_length ? " " * (header_max_length - hostname.length) : ""
  @header_end     = " -- "
  @util           = Util.new(debug)
end

Instance Method Details

#goObject



18
19
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
58
59
60
61
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
92
93
94
95
96
97
98
99
# File 'lib/worker.rb', line 18

def go
  @util.show_summary(self)

  result = ''
  @header = @header_begin + @header_padding + @header_end

  begin
    Net::SSH.start(@hostname, @username, :password => @password, :passphrase => @pkey_password, :non_interactive => true) do |ssh|
      channel = ssh.open_channel do |channel, success|

        # request a pseudo TTY formatted to screen width
        cols = %x{tput cols}.chomp.to_i - @header.length
        channel.request_pty(opts={:term=>'xterm',:chars_wide => cols})

        @util.dbg("sending command: #{@command}")
        channel.exec(@command)

        channel.on_data do |channel, data|

          attempts = 0

          if attempts >= 2
            raise 'failed to connect -- too many attempts'
          end

          if data =~ /Sorry, try again/
            raise 'failed to connect -- incorrect sudo password'
          end

          if data =~ /#{@username}@#{@hostname}'s password:/
            raise 'failed to connect -- password failed'
          end

          if data =~ /^\[sudo\] password for / and attempts == 0
            if @sudo_password
              channel.send_data "#{@sudo_password}\n"
            elsif @password
              channel.send_data "#{@password}\n"
            else
              raise 'failed to connect -- no sudo_password or password defined'
            end
            attempts += 1
            @util.dbg("attempts: #{attempts}")
          elsif data =~ /^\[sudo\] password for / and attempts == 1
            channel.send_data "#{@password}\n"
            attempts += 1
            @util.dbg("attempts: #{attempts}")
          end

          unless @block
            @util.display_data(@header, data)
          else
            result += data.to_s
          end

        end

      end

      channel.wait

      if @block
        @util.display_data(@header, result)
        puts "\n"
      end

    end#start

  rescue SocketError => e
    @util.display_error(e)
    puts "Failed to connect to #{@hostname}\n".red

  rescue RuntimeError => e
    @util.display_error(e)
    puts "#{@hostname} -- incorrect password, failed to connect".red

  rescue => e
    @util.display_error(e)

  end

end

#to_sObject



102
103
104
# File 'lib/worker.rb', line 102

def to_s
  "Worker: {hostname:'#{@hostname}',username:'#{@username}',password:'#{@password}',command:'#{@command}',block:'#{@block}'"
end