Module: ForemanRemoteExecutionCore::Utils

Defined in:
lib/foreman_remote_execution_core/utils.rb

Class Method Summary collapse

Class Method Details

.prune_known_hosts!(hostname, port, logger = Logger.new($stdout)) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/foreman_remote_execution_core/utils.rb', line 6

def prune_known_hosts!(hostname, port, logger = Logger.new($stdout))
  return if Net::SSH::KnownHosts.search_for(hostname).empty?

  target = if port == 22
             hostname
           else
             "[#{hostname}]:#{port}"
           end

  Open3.popen3('ssh-keygen', '-R', target) do |_stdin, stdout, _stderr, wait_thr|
    wait_thr.join
    stdout.read
  end
rescue Errno::ENOENT => e
  logger.warn("Could not remove #{hostname} from know_hosts: #{e}")
end