sconb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/sconb +5 -0
- data/lib/sconb/version.rb +3 -0
- data/lib/sconb.rb +180 -0
- data/sconb.gemspec +26 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ac47823d457985b4cebb46e6d51f439212b4ab1
|
4
|
+
data.tar.gz: 285e07d5da7a6b3ad0a282f85c52948ded36cfac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6edb836b3cd30bfee1b297b7d3f6dac6dd7471a2ff25716a44ce5a395f4dab71d9c76785db75e99ccf59eb7f7979ebcae53400c5760c139a235dd15b5b0ad93
|
7
|
+
data.tar.gz: fd8b3d6b4773168919e8c07051235403ae0daa681bde20b7a1387667a6a4fd5d28a8ece0afff658a4559224db0fb1aef02836d44da6b146d541aae65ded406e5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 k1LoW
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Sconb
|
2
|
+
|
3
|
+
Ssh CONfig Buckup tool.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sconb'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Backup .ssh/config to JSON
|
20
|
+
|
21
|
+
$ sconb dump > ssh_config.json
|
22
|
+
|
23
|
+
### Restore .ssh/config from JSON
|
24
|
+
|
25
|
+
$ sconb restore < ssh_config.json > ~/.ssh/config
|
26
|
+
|
27
|
+
### Backup .ssh/config with private keys to JSON
|
28
|
+
|
29
|
+
$ sconb dump --all > ssh_config.json
|
30
|
+
|
31
|
+
### Regenerate private keys from JSON
|
32
|
+
|
33
|
+
$ sconb keyregen < ssh_config.json
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( https://github.com/[my-github-username]/sconb/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/sconb
ADDED
data/lib/sconb.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
require "sconb/version"
|
2
|
+
require "thor"
|
3
|
+
require "net/ssh"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module Sconb
|
7
|
+
class CLI < Thor
|
8
|
+
|
9
|
+
method_option :all, :type => :boolean, :aliases => '-a', :default => false, :banner => 'dump .ssh/config and private keys.'
|
10
|
+
method_option :config, :type => :string, :aliases => '-c', :default => '~/.ssh/config', :banner => '.ssh/config path'
|
11
|
+
desc "dump > dump.json", "Dump .ssh/config to JSON"
|
12
|
+
def dump()
|
13
|
+
path = options[:config]
|
14
|
+
file = File.expand_path(path)
|
15
|
+
configs = {}
|
16
|
+
return configs unless File.readable?(file)
|
17
|
+
|
18
|
+
allconfig = config_load(path, '*')
|
19
|
+
configs['*'] = allconfig unless allconfig.size == 1
|
20
|
+
IO.foreach(file) do |line|
|
21
|
+
next if line =~ /^\s*(?:#.*)?$/
|
22
|
+
if line =~ /^\s*(\S+)\s*=(.*)$/
|
23
|
+
key, value = $1, $2
|
24
|
+
else
|
25
|
+
key, value = line.strip.split(/\s+/, 2)
|
26
|
+
end
|
27
|
+
next if value.nil?
|
28
|
+
next unless key.downcase == 'host'
|
29
|
+
negative_hosts, positive_hosts = value.to_s.split(/\s+/).partition { |h| h.start_with?('!') }
|
30
|
+
positive_hosts.each do | host |
|
31
|
+
config = config_load(path, host)
|
32
|
+
|
33
|
+
allconfig.each do |key, value|
|
34
|
+
next unless config.key? key
|
35
|
+
config.delete key if config[key] == allconfig[key]
|
36
|
+
end
|
37
|
+
|
38
|
+
configs[host] = config
|
39
|
+
end
|
40
|
+
end
|
41
|
+
puts JSON.pretty_generate configs
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "restore < dump.json > .ssh/config", "Restore .ssh/config from JSON"
|
45
|
+
def restore()
|
46
|
+
ssh_config = ''
|
47
|
+
json = ''
|
48
|
+
while str = $stdin.gets
|
49
|
+
json << str
|
50
|
+
end
|
51
|
+
configs = JSON.parse(json)
|
52
|
+
configs.each do |host, config|
|
53
|
+
ssh_config << 'Host ' + host + "\n"
|
54
|
+
config.each do |key, value|
|
55
|
+
next if key.downcase == 'host' || key.downcase == 'identityfilecontent'
|
56
|
+
if key.downcase == 'identityfile'
|
57
|
+
value.each_with_index do |keyfile,i|
|
58
|
+
ssh_config << ' ' + key + ' ' + keyfile + "\n"
|
59
|
+
end
|
60
|
+
else
|
61
|
+
ssh_config << ' ' + key + ' ' + value + "\n"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
ssh_config << "\n"
|
65
|
+
end
|
66
|
+
puts ssh_config
|
67
|
+
end
|
68
|
+
|
69
|
+
method_option :force, :type => :boolean, :aliases => '-f', :default => false, :banner => 'force generate'
|
70
|
+
desc "keyregen < dump.json", "Regenerate private keys from JSON"
|
71
|
+
def keyregen()
|
72
|
+
json = ''
|
73
|
+
while str = $stdin.gets
|
74
|
+
json << str
|
75
|
+
end
|
76
|
+
configs = JSON.parse(json)
|
77
|
+
configs.each do |host, config|
|
78
|
+
config.each do |key, value|
|
79
|
+
next unless key.downcase == 'identityfilecontent'
|
80
|
+
identity_files = config['IdentityFile']
|
81
|
+
value.each_with_index do |keycontent,i|
|
82
|
+
identity_file = File.expand_path(identity_files[i])
|
83
|
+
if File.exists?(identity_file) and !options[:force]
|
84
|
+
raise Thor::Error, "Error: key exists. If you want to overwrite, use --force option."
|
85
|
+
end
|
86
|
+
puts 'Regenerate ' + identity_files[i] + ' ...'
|
87
|
+
File.open(identity_file, 'w') do |file|
|
88
|
+
file.write keycontent
|
89
|
+
end
|
90
|
+
File.chmod(0600, identity_file)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Original code Net::SSH::Config.load
|
97
|
+
private
|
98
|
+
def config_load(path, host)
|
99
|
+
settings = {}
|
100
|
+
file = File.expand_path(path)
|
101
|
+
return settings unless File.readable?(file)
|
102
|
+
|
103
|
+
globals = {}
|
104
|
+
matched_host = nil
|
105
|
+
multi_host = []
|
106
|
+
seen_host = false
|
107
|
+
IO.foreach(file) do |line|
|
108
|
+
next if line =~ /^\s*(?:#.*)?$/
|
109
|
+
|
110
|
+
if line =~ /^\s*(\S+)\s*=(.*)$/
|
111
|
+
key, value = $1, $2
|
112
|
+
else
|
113
|
+
key, value = line.strip.split(/\s+/, 2)
|
114
|
+
end
|
115
|
+
|
116
|
+
# silently ignore malformed entries
|
117
|
+
next if value.nil?
|
118
|
+
|
119
|
+
value = $1 if value =~ /^"(.*)"$/
|
120
|
+
|
121
|
+
if key.downcase == 'host'
|
122
|
+
# Support "Host host1 host2 hostN".
|
123
|
+
# See http://github.com/net-ssh/net-ssh/issues#issue/6
|
124
|
+
negative_hosts, positive_hosts = value.to_s.split(/\s+/).partition { |h| h.start_with?('!') }
|
125
|
+
|
126
|
+
# Check for negative patterns first. If the host matches, that overrules any other positive match.
|
127
|
+
# The host substring code is used to strip out the starting "!" so the regexp will be correct.
|
128
|
+
negative_match = negative_hosts.select { |h| host =~ pattern2regex(h[1..-1]) }.first
|
129
|
+
|
130
|
+
if negative_match
|
131
|
+
matched_host = nil
|
132
|
+
else
|
133
|
+
matched_host = positive_hosts.select { |h| host =~ pattern2regex(h) }.first
|
134
|
+
end
|
135
|
+
|
136
|
+
seen_host = true
|
137
|
+
settings[key] = host
|
138
|
+
elsif !seen_host
|
139
|
+
if key.downcase == 'identityfile'
|
140
|
+
(globals[key] ||= []) << value
|
141
|
+
|
142
|
+
# Read IdentityFile Content
|
143
|
+
identity_file = File.expand_path(value)
|
144
|
+
if options[:all] and File.readable?(identity_file)
|
145
|
+
(globals['IdentityFileContent'] ||= []) << File.open(identity_file).read
|
146
|
+
end
|
147
|
+
else
|
148
|
+
globals[key] = value unless settings.key?(key)
|
149
|
+
end
|
150
|
+
elsif !matched_host.nil?
|
151
|
+
if key.downcase == 'identityfile'
|
152
|
+
(settings[key] ||= []) << value
|
153
|
+
|
154
|
+
# Read IdentityFile Content
|
155
|
+
identity_file = File.expand_path(value)
|
156
|
+
if options[:all] and File.readable?(identity_file)
|
157
|
+
(settings['IdentityFileContent'] ||= []) << File.open(identity_file).read
|
158
|
+
end
|
159
|
+
else
|
160
|
+
settings[key] = value unless settings.key?(key)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
settings = globals.merge(settings) if globals
|
166
|
+
|
167
|
+
return settings
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
171
|
+
# Original code Net::SSH::Config.pattern2regex
|
172
|
+
def pattern2regex(pattern)
|
173
|
+
pattern = "^" + pattern.to_s.gsub(/\./, "\\.").
|
174
|
+
gsub(/\?/, '.').
|
175
|
+
gsub(/([+\/])/, '\\\\\\0').
|
176
|
+
gsub(/\*/, '.*') + "$"
|
177
|
+
Regexp.new(pattern, true)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
data/sconb.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sconb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sconb"
|
8
|
+
spec.version = Sconb::VERSION
|
9
|
+
spec.authors = ["k1LoW"]
|
10
|
+
spec.email = ["k1lowxb@gmail.com"]
|
11
|
+
spec.summary = %q{Ssh CONfig Buckup tool.}
|
12
|
+
spec.description = %q{Ssh CONfig Buckup tool.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "net-ssh"
|
22
|
+
spec.add_runtime_dependency "json"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_dependency "thor"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sconb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- k1LoW
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-ssh
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: thor
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Ssh CONfig Buckup tool.
|
84
|
+
email:
|
85
|
+
- k1lowxb@gmail.com
|
86
|
+
executables:
|
87
|
+
- sconb
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/sconb
|
97
|
+
- lib/sconb.rb
|
98
|
+
- lib/sconb/version.rb
|
99
|
+
- sconb.gemspec
|
100
|
+
homepage: ''
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Ssh CONfig Buckup tool.
|
124
|
+
test_files: []
|