Module: Sshx::Cli
- Defined in:
- lib/sshx/cli.rb
Constant Summary collapse
- @@home_directory =
File.('~')
- @@namespace_separator =
'.'- @@enable_alias =
true- @@ssh_path =
`which ssh`
- @@ssh_config_path =
@@home_directory + '/.ssh/config'
- @@sshx_config_path =
@@home_directory + '/.sshx/config'
Class Method Summary collapse
- .get_hostname_in_args(args) ⇒ Object
- .get_hosts ⇒ Object
- .init ⇒ Object
- .load_config ⇒ Object
- .make_alias_commands ⇒ Object
- .make_commands ⇒ Object
- .make_complete_commands(hosts) ⇒ Object
- .make_ssh_config ⇒ Object
- .run_tmux(commands) ⇒ Object
- .start(args = ARGV) ⇒ Object
Class Method Details
.get_hostname_in_args(args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sshx/cli.rb', line 61 def get_hostname_in_args(args) is_option_parameter = false args.each{|arg| if /^-/ =~ arg is_option_parameter = true next elsif is_option_parameter is_option_parameter = false next else return arg end } end |
.get_hosts ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/sshx/cli.rb', line 226 def get_hosts() hosts = [] open(@@ssh_config_path) {|file| while line = file.gets line = line.chomp matches = line.scan(/Host\s+([^\s]+)/i) if matches.length == 0 next end hosts.push(matches[0][0]) end } return hosts end |
.init ⇒ Object
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/sshx/cli.rb', line 79 def init() if File.exist?(@@home_directory + '/.sshx') return true end puts "\e[36m" puts ' ------------------------- ' puts ' #### #### # # # # ' puts ' # # # # # # ' puts ' ### ### ##### # ' puts ' # # # # # # ' puts ' #### #### # # # # ' puts ' ------------------------- ' puts ' Welcome to sshx! ' puts "\e[0m" puts 'Initialize sshx...' puts 'Import ssh config file...' Dir.mkdir(@@home_directory + '/.sshx') FileUtils.cp(@@home_directory + '/.ssh/config', @@home_directory + '/.sshx/ssh_config') puts 'Make config file...' File.open(@@home_directory + '/.sshx/config', 'w'){|file| file.puts('NamespaceSeparator ' + @@namespace_separator) file.puts('EnableAlias ' + (@@enable_alias?'true':'false')) file.puts('SshPath ' + @@ssh_path) } puts 'Edit .bashrc file...' bashrc_path = nil initial_commands = [] initial_commands.push('# Initialize sshx') initial_commands.push('eval "$(sshx init -)"') initial_command = initial_commands.join("\n") if File.exist?(@@home_directory + '/.bashrc') bashrc_path = @@home_directory + '/.bashrc' elsif File.exist?(@@home_directory + '/.bash_profile') bashrc_path = @@home_directory + '/.bash_profile' else puts "\e[33m[ERROR] Failed to find ~/.bashrc or ~/.bash_profile. The following command should be run at the begining of shell.\e[0m" puts '' puts initial_command puts '' return false end File.open(bashrc_path, 'a'){|file| file.puts(initial_command) } puts 'Successfully initialized.' puts '' return true end |
.load_config ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/sshx/cli.rb', line 141 def load_config() file = open(@@home_directory + '/.sshx/config') while line = file.gets line = line.chomp matches = line.scan(/NamespaceSeparator\s+([^\s]*)/i) if matches.length > 0 @@namespace_separator = matches[0][0] next end matches = line.scan(/EnableAlias\s+([^\s]*)/i) if matches.length > 0 @@enable_alias = (matches[0][0] =~ /true$/i ? true : false) next end matches = line.scan(/SshPath\s+([^\s]*)/i) if matches.length > 0 @@ssh_path = matches[0][0] next end end file.close end |
.make_alias_commands ⇒ Object
274 275 276 277 278 279 280 281 282 283 |
# File 'lib/sshx/cli.rb', line 274 def make_alias_commands() commands = []; commands.push('alias ssh=sshx') commands.push('complete -F _sshx ssh') return commands end |
.make_commands ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/sshx/cli.rb', line 249 def make_commands() commands = [] hosts = get_hosts() commands.concat(make_complete_commands(hosts)) if @@enable_alias commands.concat(make_alias_commands()) end return commands end |
.make_complete_commands(hosts) ⇒ Object
263 264 265 266 267 268 269 270 271 272 |
# File 'lib/sshx/cli.rb', line 263 def make_complete_commands(hosts) commands = []; commands.push('_sshx(){ COMPREPLY=($(compgen -W "' + hosts.join(' ') + '" ${COMP_WORDS[COMP_CWORD]})) ; }') commands.push('complete -F _sshx sshx') return commands end |
.make_ssh_config ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/sshx/cli.rb', line 171 def make_ssh_config() @@home_directory = File.('~') configs = [] configs.push('#############################################################') configs.push('# CAUTION!') configs.push('# This config is auto generated by sshx') configs.push('# You cannot edit this file. Edit ~/.sshx/ssh_config insted.') configs.push('#############################################################') configs.push('') Dir::foreach(@@home_directory + '/.sshx/') {|file_path| if /^\./ =~ file_path next end if /^config$/i =~ file_path next end file = open(@@home_directory + '/.sshx/' + file_path) namespace = nil while line = file.gets line = line.chomp matches = line.scan(/Namespace\s+([^\s]+)/i) if matches.length > 0 namespace = matches[0][0] next end if namespace line = line.gsub(/(Host\s+)([^\s]+)/i, '\1' + namespace + @@namespace_separator + '\2') end configs.push(line) end file.close } file = open(@@ssh_config_path, 'w') file.write(configs.join("\n")) file.close end |
.run_tmux(commands) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/sshx/cli.rb', line 285 def run_tmux(commands) tmux_path = `which tmux` if $?.exitstatus > 0 puts '[ERROR] tmux must be installed to use multi host connection. Install tmux from the following url. http://tmux.sourceforge.net/' exit 1 end window_name = 'sshx-window' session_name = 'sshx-session-' + Time.now.to_i.to_s + Time.now.usec.to_s layout = 'tiled' `tmux start-server` `tmux new-session -d -n #{window_name} -s #{session_name}` commands.each_with_index { |command, index| if index > 0 `tmux split-window -v -t #{window_name}` end escaped_command = command.shellescape `tmux send-keys #{escaped_command} C-m` `tmux select-layout -t #{window_name} #{layout}` } `tmux set-window-option synchronize-panes on` `tmux attach-session -t #{session_name}` end |
.start(args = ARGV) ⇒ Object
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 |
# File 'lib/sshx/cli.rb', line 14 def start(args = ARGV) if !init() exit 1 end load_config() make_ssh_config() if args.length == 0 puts 'sshx is just a wrapper of ssh.' puts `#{@@ssh_path}` return end if args.length == 2 && args[0] == 'init' && args[1] == '-' puts make_commands().join("\n") return end commands = [] hostname_arg = get_hostname_in_args(args) hostnames = hostname_arg.split(/\s*,\s*/) ssh_command = hostnames.length >= 2 ? $0 : @@ssh_path hostnames.each{|hostname| shell_args = [] args.each{|arg| if arg == hostname_arg shell_args.push(hostname.shellescape) next end shell_args.push(arg.shellescape) } commands.push(ssh_command + ' ' + shell_args.join(' ')) } if commands.length == 1 system(commands[0]) else run_tmux(commands) end status = $?.exitstatus exit status end |