sshx 0.1.1 → 0.2.0

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.
Files changed (4) hide show
  1. data/README.md +20 -2
  2. data/lib/sshx/cli.rb +67 -5
  3. data/lib/sshx/version.rb +1 -1
  4. metadata +1 -1
data/README.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  Extended ssh command to use multi ssh_config, namespace and command completion.
4
4
 
5
+ ## Installation
6
+
7
+ You can install sshx from RubyGems.
8
+
9
+ ```bash
10
+ gem install sshx
11
+ ```
12
+
5
13
  ## Usage
6
14
 
7
15
  You can use the sshx in the same way as ssh command because sshx is just a wrapper of ssh.
@@ -16,10 +24,20 @@ While ssh has only one configuration file .ssh/config, sshx can have multi confi
16
24
 
17
25
  ```bash
18
26
  $ ls ~/.sshx/
19
- album blog config
27
+ album blog config ssh_config
28
+ ```
29
+
30
+ The config file is configuration files for sshx. The album, blog and ssh_config (It's imported from ~/.ssh/config) are configuration for ssh.
31
+
32
+ ## Multi hosts connection with tmux
33
+
34
+ You can connect to some hosts with [tmux](http://tmux.sourceforge.net/).
35
+
36
+ ```bash
37
+ sshx blog.prd.web1,blog.prd.web2,blog.prd.web3
20
38
  ```
21
39
 
22
- The album and blog are configuration files for sshx.
40
+ tmux must be installed if you use multi hosts connection.
23
41
 
24
42
  ## Namespace
25
43
 
@@ -31,18 +31,51 @@ module Sshx
31
31
  return
32
32
  end
33
33
 
34
- shell_args = []
35
- args.each{|arg|
36
- shell_args.push(arg.shellescape)
34
+ commands = []
35
+ hostname_arg = get_hostname_in_args(args)
36
+ hostnames = hostname_arg.split(/\s*,\s*/)
37
+ ssh_command = hostnames.length >= 2 ? $0 : @@ssh_path
38
+ hostnames.each{|hostname|
39
+ shell_args = []
40
+ args.each{|arg|
41
+ if arg == hostname_arg
42
+ shell_args.push(hostname.shellescape)
43
+ next
44
+ end
45
+ shell_args.push(arg.shellescape)
46
+ }
47
+ commands.push(ssh_command + ' ' + shell_args.join(' '))
37
48
  }
38
49
 
39
- system(@@ssh_path + ' ' + shell_args.join(' '))
40
- status = $?.exitstatus
50
+ if commands.length == 1
51
+ system(commands[0])
52
+ else
53
+ run_tmux(commands)
54
+ end
41
55
 
56
+ status = $?.exitstatus
42
57
  exit status
43
58
 
44
59
  end
45
60
 
61
+ def get_hostname_in_args(args)
62
+
63
+ is_option_parameter = false
64
+
65
+ args.each{|arg|
66
+ if /^-/ =~ arg
67
+ is_option_parameter = true
68
+ next
69
+ elsif is_option_parameter
70
+ is_option_parameter = false
71
+ next
72
+ else
73
+ return arg
74
+ end
75
+ }
76
+
77
+ end
78
+
46
79
  def init()
47
80
 
48
81
  if File.exist?(@@home_directory + '/.sshx')
@@ -249,6 +282,35 @@ module Sshx
249
282
 
250
283
  end
251
284
 
285
+ def run_tmux(commands)
286
+
287
+ tmux_path = `which tmux`
288
+ if $?.exitstatus > 0
289
+ puts '[ERROR] tmux must be installed to use multi host connection. Install tmux from the following url. http://tmux.sourceforge.net/'
290
+ exit 1
291
+ end
292
+
293
+ window_name = 'sshx-window'
294
+ session_name = 'sshx-session-' + Time.now.to_i.to_s + Time.now.usec.to_s
295
+ layout = 'tiled'
296
+
297
+ `tmux start-server`
298
+ `tmux new-session -d -n #{window_name} -s #{session_name}`
299
+
300
+ commands.each_with_index { |command, index|
301
+ if index > 0
302
+ `tmux split-window -v -t #{window_name}`
303
+ end
304
+ escaped_command = command.shellescape
305
+ `tmux send-keys #{escaped_command} C-m`
306
+ `tmux select-layout -t #{window_name} #{layout}`
307
+ }
308
+
309
+ `tmux set-window-option synchronize-panes on`
310
+ `tmux attach-session -t #{session_name}`
311
+
312
+ end
313
+
252
314
  end
253
315
  end
254
316
  end
@@ -1,3 +1,3 @@
1
1
  module Sshx
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: