sshx 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/LICENSE +21 -0
- data/README.md +57 -0
- data/bin/sshx +4 -0
- data/lib/sshx.rb +3 -0
- data/lib/sshx/cli.rb +253 -0
- data/lib/sshx/version.rb +3 -0
- data/sshx.gemspec +15 -0
- metadata +54 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 katty0324
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# sshx
|
2
|
+
|
3
|
+
Extended ssh command to use multi ssh_config, namespace and command completion.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
You can use the sshx in the same way as ssh command because sshx is just a wrapper of ssh.
|
8
|
+
|
9
|
+
```bash
|
10
|
+
sshx hostname
|
11
|
+
```
|
12
|
+
|
13
|
+
## Multi configuration files
|
14
|
+
|
15
|
+
While ssh has only one configuration file .ssh/config, sshx can have multi configuration files in .sshx directory.
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ ls ~/.sshx/
|
19
|
+
album blog config
|
20
|
+
```
|
21
|
+
|
22
|
+
The album and blog are configuration files for sshx.
|
23
|
+
|
24
|
+
## Namespace
|
25
|
+
|
26
|
+
Syntax of sshx configuration files is the superset of ssh. It supports namespace additionally.
|
27
|
+
|
28
|
+
```
|
29
|
+
Namespace blog
|
30
|
+
|
31
|
+
Host prd.web
|
32
|
+
HostName blog.katty.in
|
33
|
+
Port 22
|
34
|
+
User katty0324
|
35
|
+
IdentityFile ~/.ssh/id_rsa
|
36
|
+
```
|
37
|
+
|
38
|
+
Then you can use following command.
|
39
|
+
|
40
|
+
```bash
|
41
|
+
sshx blog.prd.web
|
42
|
+
```
|
43
|
+
|
44
|
+
## Command completion
|
45
|
+
|
46
|
+
Command completion is also supported in sshx.
|
47
|
+
|
48
|
+
```bash
|
49
|
+
$ sshx blog.p
|
50
|
+
# If You type [TAB] here
|
51
|
+
$ sshx blog.prd.web
|
52
|
+
# the hostname will be compeleted.
|
53
|
+
```
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
This tool is under MIT license.
|
data/bin/sshx
ADDED
data/lib/sshx.rb
ADDED
data/lib/sshx/cli.rb
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'shellwords'
|
3
|
+
|
4
|
+
module Sshx
|
5
|
+
module Cli
|
6
|
+
class << self
|
7
|
+
|
8
|
+
@@home_directory = File.expand_path('~')
|
9
|
+
@@namespace_separator = '.'
|
10
|
+
@@enable_alias = true
|
11
|
+
@@ssh_path = `which ssh`
|
12
|
+
@@temporary_config_path = '/tmp/sshx_config'
|
13
|
+
def start(args = ARGV)
|
14
|
+
|
15
|
+
if !init()
|
16
|
+
exit 1
|
17
|
+
end
|
18
|
+
|
19
|
+
load_config()
|
20
|
+
|
21
|
+
if args.length == 0
|
22
|
+
puts 'sshx is just a wrapper of ssh.'
|
23
|
+
puts `#{@@ssh_path}`
|
24
|
+
return
|
25
|
+
end
|
26
|
+
|
27
|
+
make_temporary_config()
|
28
|
+
|
29
|
+
if args.length == 2 && args[0] == 'init' && args[1] == '-'
|
30
|
+
puts make_commands().join("\n")
|
31
|
+
remove_temporary_config()
|
32
|
+
return
|
33
|
+
end
|
34
|
+
|
35
|
+
shell_args = []
|
36
|
+
args.each{|arg|
|
37
|
+
shell_args.push(arg.shellescape)
|
38
|
+
}
|
39
|
+
|
40
|
+
system(@@ssh_path + ' ' + shell_args.join(' ') + ' -F ' + @@temporary_config_path)
|
41
|
+
status = $?.exitstatus
|
42
|
+
|
43
|
+
remove_temporary_config()
|
44
|
+
|
45
|
+
exit status
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def init()
|
50
|
+
|
51
|
+
if File.exist?(@@home_directory + '/.sshx')
|
52
|
+
return true
|
53
|
+
end
|
54
|
+
|
55
|
+
puts "\e[36m"
|
56
|
+
puts ' ------------------------- '
|
57
|
+
puts ' #### #### # # # # '
|
58
|
+
puts ' # # # # # # '
|
59
|
+
puts ' ### ### ##### # '
|
60
|
+
puts ' # # # # # # '
|
61
|
+
puts ' #### #### # # # # '
|
62
|
+
puts ' ------------------------- '
|
63
|
+
puts ' Welcome to sshx! '
|
64
|
+
puts "\e[0m"
|
65
|
+
puts 'Initialize sshx...'
|
66
|
+
|
67
|
+
puts 'Import ssh config file...'
|
68
|
+
|
69
|
+
Dir.mkdir(@@home_directory + '/.sshx')
|
70
|
+
FileUtils.symlink(@@home_directory + '/.ssh/config', @@home_directory + '/.sshx/ssh_config')
|
71
|
+
|
72
|
+
puts 'Make config file...'
|
73
|
+
|
74
|
+
File.open(@@home_directory + '/.sshx/config', 'w'){|file|
|
75
|
+
file.puts('NamespaceSeparator ' + @@namespace_separator)
|
76
|
+
file.puts('EnableAlias ' + (@@enable_alias?'true':'false'))
|
77
|
+
file.puts('SshPath ' + @@ssh_path)
|
78
|
+
file.puts('TemporaryConfigPath ' + @@temporary_config_path)
|
79
|
+
}
|
80
|
+
|
81
|
+
puts 'Edit .bashrc file...'
|
82
|
+
|
83
|
+
bashrc_path = nil
|
84
|
+
initial_commands = []
|
85
|
+
initial_commands.push('# Initialize sshx')
|
86
|
+
initial_commands.push('eval "$(sshx init -)"')
|
87
|
+
initial_command = initial_commands.join("\n")
|
88
|
+
|
89
|
+
if File.exist?(@@home_directory + '/.bashrc')
|
90
|
+
bashrc_path = @@home_directory + '/.bashrc'
|
91
|
+
elsif File.exist?(@@home_directory + '/.bash_profile')
|
92
|
+
bashrc_path = @@home_directory + '/.bash_profile'
|
93
|
+
else
|
94
|
+
puts "\e[33m[ERROR] Failed to find ~/.bashrc or ~/.bash_profile. The following command should be run at the begining of shell.\e[0m"
|
95
|
+
puts ''
|
96
|
+
puts initial_command
|
97
|
+
puts ''
|
98
|
+
return false
|
99
|
+
end
|
100
|
+
|
101
|
+
File.open(bashrc_path, 'a'){|file|
|
102
|
+
file.puts(initial_command)
|
103
|
+
}
|
104
|
+
|
105
|
+
puts 'Successfully initialized.'
|
106
|
+
puts ''
|
107
|
+
|
108
|
+
return true
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
def load_config()
|
113
|
+
|
114
|
+
file = open(@@home_directory + '/.sshx/config')
|
115
|
+
while line = file.gets
|
116
|
+
|
117
|
+
matches = line.scan(/NamespaceSeparator\s+([^\s]*)/i)
|
118
|
+
if matches.length > 0
|
119
|
+
@@namespace_separator = matches[0][0]
|
120
|
+
next
|
121
|
+
end
|
122
|
+
|
123
|
+
matches = line.scan(/EnableAlias\s+([^\s]*)/i)
|
124
|
+
if matches.length > 0
|
125
|
+
@@enable_alias = (matches[0][0] =~ /true$/i ? true : false)
|
126
|
+
next
|
127
|
+
end
|
128
|
+
|
129
|
+
matches = line.scan(/SshPath\s+([^\s]*)/i)
|
130
|
+
if matches.length > 0
|
131
|
+
@@ssh_path = matches[0][0]
|
132
|
+
next
|
133
|
+
end
|
134
|
+
|
135
|
+
matches = line.scan(/TemporaryConfigPath\s+([^\s]*)/i)
|
136
|
+
if matches.length > 0
|
137
|
+
@@temporary_config_path = matches[0][0]
|
138
|
+
next
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
file.close
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
def make_temporary_config()
|
147
|
+
|
148
|
+
@@home_directory = File.expand_path('~')
|
149
|
+
|
150
|
+
configs = []
|
151
|
+
Dir::foreach(@@home_directory + '/.sshx/') {|file_path|
|
152
|
+
|
153
|
+
if /^\./ =~ file_path
|
154
|
+
next
|
155
|
+
end
|
156
|
+
|
157
|
+
if /^config$/i =~ file_path
|
158
|
+
next
|
159
|
+
end
|
160
|
+
|
161
|
+
file = open(@@home_directory + '/.sshx/' + file_path)
|
162
|
+
|
163
|
+
namespace = nil
|
164
|
+
|
165
|
+
while line = file.gets
|
166
|
+
|
167
|
+
matches = line.scan(/Namespace\s+([^\s]+)/i)
|
168
|
+
if matches.length > 0
|
169
|
+
namespace = matches[0][0]
|
170
|
+
next
|
171
|
+
end
|
172
|
+
|
173
|
+
if namespace
|
174
|
+
line = line.gsub(/(Host\s+)([^\s]+)/i, '\1' + namespace + @@namespace_separator + '\2')
|
175
|
+
end
|
176
|
+
|
177
|
+
configs.push(line)
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
file.close
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
file = open(@@temporary_config_path, 'w')
|
186
|
+
file.write(configs.join("\n"))
|
187
|
+
file.close
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
def remove_temporary_config()
|
192
|
+
|
193
|
+
File.unlink(@@temporary_config_path)
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
def get_hosts()
|
198
|
+
|
199
|
+
hosts = []
|
200
|
+
|
201
|
+
open(@@temporary_config_path) {|file|
|
202
|
+
while line = file.gets
|
203
|
+
matches = line.scan(/Host\s+([^\s]+)/i)
|
204
|
+
if matches.length == 0
|
205
|
+
next
|
206
|
+
end
|
207
|
+
hosts.push(matches[0][0])
|
208
|
+
end
|
209
|
+
}
|
210
|
+
|
211
|
+
return hosts
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
def make_commands()
|
216
|
+
|
217
|
+
commands = []
|
218
|
+
|
219
|
+
hosts = get_hosts()
|
220
|
+
commands.concat(make_complete_commands(hosts))
|
221
|
+
if @@enable_alias
|
222
|
+
commands.concat(make_alias_commands())
|
223
|
+
end
|
224
|
+
|
225
|
+
return commands
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
def make_complete_commands(hosts)
|
230
|
+
|
231
|
+
commands = [];
|
232
|
+
|
233
|
+
commands.push('_sshx(){ COMPREPLY=($(compgen -W "' + hosts.join(' ') + '" ${COMP_WORDS[COMP_CWORD]})) ; }')
|
234
|
+
commands.push('complete -F _sshx sshx')
|
235
|
+
|
236
|
+
return commands
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
def make_alias_commands()
|
241
|
+
|
242
|
+
commands = [];
|
243
|
+
|
244
|
+
commands.push('alias ssh=sshx')
|
245
|
+
commands.push('complete -F _sshx ssh')
|
246
|
+
|
247
|
+
return commands
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
data/lib/sshx/version.rb
ADDED
data/sshx.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'sshx/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'sshx'
|
6
|
+
s.version = Sshx::VERSION
|
7
|
+
s.summary = 'Extended ssh command'
|
8
|
+
s.files = `git ls-files`.split("\n")
|
9
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
10
|
+
s.authors = ['katty0324']
|
11
|
+
s.email = 'kataoka@sirok.co.jp'
|
12
|
+
s.homepage = 'https://github.com/katty0324/sshx'
|
13
|
+
s.rubyforge_project = 'sshx'
|
14
|
+
s.description = 'Extended ssh command to use multi ssh_config, namespace and command completion.'
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sshx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- katty0324
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Extended ssh command to use multi ssh_config, namespace and command completion.
|
15
|
+
email: kataoka@sirok.co.jp
|
16
|
+
executables:
|
17
|
+
- sshx
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- bin/sshx
|
25
|
+
- lib/sshx.rb
|
26
|
+
- lib/sshx/cli.rb
|
27
|
+
- lib/sshx/version.rb
|
28
|
+
- sshx.gemspec
|
29
|
+
homepage: https://github.com/katty0324/sshx
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project: sshx
|
49
|
+
rubygems_version: 1.8.23
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Extended ssh command
|
53
|
+
test_files: []
|
54
|
+
has_rdoc:
|