Class: Hotdog::Commands::Ssh
- Inherits:
-
Search
- Object
- BaseCommand
- Search
- Hotdog::Commands::Ssh
- Defined in:
- lib/hotdog/commands/ssh.rb
Constant Summary
Constants inherited from BaseCommand
Instance Attribute Summary
Attributes inherited from BaseCommand
#application, #logger, #options
Instance Method Summary collapse
Methods inherited from Search
#evaluate, #get_hosts_with_search_tags, #parse
Methods inherited from BaseCommand
#execute, #fixed_string?, #initialize
Constructor Details
This class inherits a constructor from Hotdog::Commands::BaseCommand
Instance Method Details
#run(args = []) ⇒ Object
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 |
# File 'lib/hotdog/commands/ssh.rb', line 11 def run(args=[]) ssh_option = { index: nil, options: [], user: nil, port: nil, identity_file: nil, forward_agent: false, } optparse.on("-n", "--index INDEX", "Use this index of host if multiple servers are found", Integer) do |index| ssh_option[:index] = index end optparse.on("-o SSH_OPTION", "Passes this string to ssh command through shell. This option may be given multiple times") do |option| ssh_option[:options] += [option] end optparse.on("-i SSH_IDENTITY_FILE", "SSH identity file path") do |path| ssh_option[:identity_file] = path end optparse.on("-A", "Enable agent forwarding", TrueClass) do |b| ssh_option[:forward_agent] = b end optparse.on("-p PORT", "Port of the remote host", Integer) do |port| ssh_option[:port] = port end optparse.on("-u SSH_USER", "SSH login user name") do |user| ssh_option[:user] = user end search_args = [] optparse.order!(args) {|search_arg| search_args.push(search_arg) } expression = search_args.join(" ").strip if expression.empty? exit(1) end begin node = parse(expression) rescue Parslet::ParseFailed => error STDERR.puts("syntax error: " + error.cause.ascii_tree) exit(1) end result = evaluate(node, self).sort if result.length == 1 host = result[0] elsif result.empty? STDERR.puts("no match found: #{search_args.join(" ")}") exit(1) else if ssh_option[:index] && result.length > ssh_option[:index] host = result[ssh_option[:index]] else result, fields = (result, node) # add "index" field result = result.each_with_index.map {|host,i| [i] + host } fields = ["index"] + fields STDERR.print(format(result, fields: fields)) logger.info("found %d host(s)." % result.length) exit(1) end end result, fields = get_hosts([host]) address = result.flatten.first # build ssh command cmdline = ["ssh"] ssh_option[:options].each do |option| cmdline << "-o" << option end if path = ssh_option[:identity_file] cmdline << "-i" << Shellwords.escape(path) end if port = ssh_option[:port] cmdline << "-p" << port.to_s end if ssh_option[:forward_agent] cmdline << "-A" end if user = ssh_option[:user] cmdline << (user + "@" + address) else cmdline << address end cmdline.concat(args) exec *cmdline exit(127) end |