Class: Russh::Accessor

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

Instance Method Summary collapse

Constructor Details

#initializeAccessor

Returns a new instance of Accessor.



6
7
8
9
# File 'lib/russh/accessor.rb', line 6

def initialize
  @path = Dir.home + '/.ssh/config'
  @is_existing = is_existing?
end

Instance Method Details

#backupObject

Backups the config file to config.bk



22
23
24
# File 'lib/russh/accessor.rb', line 22

def backup
  FileUtils.cp @path, "#{@path}.bk"
end

#create(host, host_name, user) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# 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?Boolean

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

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
# 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

#listObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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