class Russh::Accessor

Public Class Methods

new() click to toggle source
# File lib/russh/accessor.rb, line 6
def initialize
  @path = Dir.home + '/.ssh/config'
  @is_existing = is_existing?
end

Public Instance Methods

backup() click to toggle source

Backups the config file to config.bk

# File lib/russh/accessor.rb, line 22
def backup
  FileUtils.cp @path, "#{@path}.bk"
end
create(host, host_name, user) click to toggle source
# File lib/russh/accessor.rb, line 26
def create(host, host_name, user)
  @host = host
  @host_name = host_name
  @user = user
  backup
  open(@path, 'a+') do |f|
    # If the file is new don't add a newline.
    f.puts if f.readlines.size > 0
    # Format each entry
    f.puts format_entry
  end
end
is_existing?() click to toggle source

Checks if the config file is existing. If not it creates a new one.

# File lib/russh/accessor.rb, line 12
def is_existing?
  if File.exist? @path
    @is_existing = true
  else
    puts "You don't have an existing ssh config file. Creating a new one for you."
    File.new(@path, 'w')
  end
end
list() click to toggle source
# File lib/russh/accessor.rb, line 39
def list
  hosts = []
  configs = []

  File.readlines(@path).each do |line|
    hosts << line.split(' ')[1].strip if line[/^Host/]
  end

  hosts.each do |host|
     configs.push Net::SSH::Config.load @path, host
  end

  configs.each do |config|
    puts "Host #{config['host']} HostName #{config['hostname']} User #{config['user']}"
  end

end

Protected Instance Methods

format_entry() click to toggle source

Formats each entry

# File lib/russh/accessor.rb, line 60
def format_entry
  "Host #{@host}\n\tHostName #{@host_name}\n\tUser #{@user}"
end