This repository has been archived by the owner on Nov 28, 2018. It is now read-only.
/
mc-ssh
74 lines (58 loc) · 1.67 KB
/
mc-ssh
1
2
3
4
5
6
7
8
9
10
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
#!/usr/bin/env ruby
# Frontend to ssh that uses mcollective discovery to find hosts.
#
# It requires the rdialog gem to be available, basic usage is:
#
# mc-ssh --with-class /webserver/ -- -l root
#
# This will present you with a list of hosts and run:
#
# ssh <host> -l root
#
# on your chosen host.
#
# Released under Apache Licence 2, R.I.Pienaar <rip@devco.net>
#
# https://github.com/puppetlabs/mcollective-plugins
require 'mcollective'
require 'rdialog'
include MCollective::RPC
options = rpcoptions do |parser, options|
parser.define_head "MCollective discovery enabled ssh"
parser.separator ""
parser.separator "Usage: mc-ssh [filters and options] -- [ssh options]"
end
if ARGV.length >= 1
sshoptions = ARGV.join(" ")
end
client = rpcclient("discovery", :options => options)
@dialog = RDialog.new
@dialog.backtitle = "Marionette Collective SSH"
class CancelPressed<Exception; end
# Displays some text while you do something in the background
def infobox(msg, title, height = 5, width = 40)
@dialog.title = "\"#{title}\""
@dialog.infobox("\n" + msg, height, width)
end
# Shows a list of options and lets the user choose one
def choose(choices, title)
res = @dialog.menu(title, choices)
raise CancelPressed.new unless res
res
end
infobox("Doing discovery", "Marionette Collective SSH")
choices = []
client.discover.each_with_index do |host,i|
choices << [i, host]
end
begin
hostname = choose(choices, "").to_i
hostname = choices[hostname][1]
puts("Running: ssh #{hostname} #{sshoptions}")
exec("ssh #{hostname} #{sshoptions}")
rescue CancelPressed
exit!
rescue Exception => e
puts("Failed to run mc-ssh: #{e}")
exit!
end