Class: SSHKit::Sudo::DefaultInteractionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/sudo/interaction_handler.rb

Direct Known Subclasses

InteractionHandler

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.password_promptObject



36
37
38
# File 'lib/sshkit/sudo/interaction_handler.rb', line 36

def password_prompt
  @password_prompt ||= /[Pp]assword.*:/
end

.password_prompt_regexp(regexp) ⇒ Object



44
45
46
# File 'lib/sshkit/sudo/interaction_handler.rb', line 44

def password_prompt_regexp(regexp)
  @password_prompt = regexp
end

.use_same_password!Object



48
49
50
51
52
53
54
# File 'lib/sshkit/sudo/interaction_handler.rb', line 48

def use_same_password!
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
  def password_cache_key(host)
    '0'
  end
  METHOD
end

.wrong_passwordObject



32
33
34
# File 'lib/sshkit/sudo/interaction_handler.rb', line 32

def wrong_password
  @wrong_password ||= /Sorry.*\stry\sagain/
end

.wrong_password_regexp(regexp) ⇒ Object



40
41
42
# File 'lib/sshkit/sudo/interaction_handler.rb', line 40

def wrong_password_regexp(regexp)
  @wrong_password = regexp
end

Instance Method Details

#on_data(command, stream_name, data, channel) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sshkit/sudo/interaction_handler.rb', line 7

def on_data(command, stream_name, data, channel)
  if data =~ wrong_password
    puts data if defined?(Airbrussh) and
                 Airbrussh.configuration.command_output != :stdout and
                 data !~ password_prompt
    SSHKit::Sudo.password_cache[password_cache_key(command.host)] = nil
  end
  if data =~ password_prompt
    key = password_cache_key(command.host)
    pass = SSHKit::Sudo.password_cache[key]
    unless pass
      print data
      pass = $stdin.noecho(&:gets)
      puts ''
      SSHKit::Sudo.password_cache[key] = pass
    end
    channel.send_data(pass)
  end
end

#password_cache_key(host) ⇒ Object



27
28
29
# File 'lib/sshkit/sudo/interaction_handler.rb', line 27

def password_cache_key(host)
  "#{host.user}@#{host.hostname}"
end

#password_promptObject



5
# File 'lib/sshkit/sudo/interaction_handler.rb', line 5

def password_prompt; self.class.password_prompt; end

#wrong_passwordObject



4
# File 'lib/sshkit/sudo/interaction_handler.rb', line 4

def wrong_password; self.class.wrong_password; end