Class: DevopsToolkit::Tasks::SSH

Inherits:
Thor
  • Object
show all
Defined in:
lib/devops_toolkit/tasks/ssh.rb

Constant Summary collapse

DESC =
'SSH and SSH configuration tasks'
COMMAND =
'ssh'

Instance Method Summary collapse

Instance Method Details

#known_hostsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/devops_toolkit/tasks/ssh.rb', line 11

def known_hosts
  full_known_hosts_path = File.expand_path(options[:known_hosts])
  puts "Removing line #{options[:line]} from #{full_known_hosts_path}"
  begin
    if !options[:line].is_a?(Integer)
      raise ArgumentError, 'line must be an integer'
    end
    # Should be not too big of a file, so we probably
    # can read all of it to memory
    lines = File.readlines(full_known_hosts_path).map {|l| l.strip}
    deleted_line = lines.delete_at(options[:line] - 1)
    puts "Deleted: #{deleted_line}"
    File.write(full_known_hosts_path, "#{lines.join("\n")}\n")
  rescue Exception => e
    puts "#{e.class}: #{e.message}"
  end
end