This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ssh_config.rb
110 lines (85 loc) · 2.38 KB
/
ssh_config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module Ghost
class SshConfigDuplicateError < StandardError; end
class SshConfig
@@ssh_config = "#{ENV['HOME']}/.ssh/config"
attr_accessor :host, :hostname, :user, :port
alias :to_s :host
def initialize(host, hostname, user, port)
@host = host
@hostname = hostname
@user = user
@port = port
end
class << self
protected :new
def list
lines = File.read(@@ssh_config).split("\n")
entries = []
current_entry = []
lines << ""
lines.each do |line|
if line.strip.empty?
entries << parse(current_entry) if current_entry.any?
current_entry = []
else
current_entry << line
end
end
entries
end
def add(args)
host = args[:host]
hostname = args[:hostname]
user = args[:user] || "root"
port = args[:port] || "22"
force = args[:force]
if find_by_host(host).nil? || force
delete(host)
new_config = SshConfig.new(host, hostname, user, port)
configs = list
configs << new_config
write(configs)
new_config
else
raise SshConfigDuplicateError, "Can not overwrite existing record"
end
end
def find_by_host(host)
list.find { |c| c.host == host }
end
def empty!
write([])
end
def delete(host)
configs = list
configs = configs.delete_if { |c| c.host == host }
write(configs)
end
def write(configs)
hosts = []
configs.sort! { |a,b| a.host <=> b.host }
configs.each do |c|
hosts << "Host #{c.host}"
hosts << " HostName #{c.hostname}"
hosts << " User #{c.user}" if c.user
hosts << " Port #{c.port}" if c.port
hosts << ""
end
File.open(@@ssh_config, 'w') {|f| f.print hosts.join("\n") }
end
def parse(config)
host = config.first[/Host (.*)/, 1]
config_hash = {}
config[1..-1].each do |entry|
entry.strip!
next if entry.empty?
key, value = entry.strip.split(" ")
config_hash[key.downcase] = value
end
SshConfig.new(host,
config_hash['hostname'], config_hash['user'],
config_hash['port'])
end
end
end
end