Ruby hm - ssh for cluster management
#!ruby
#
# $Id$
#
# /home/madann/learn/ruby/hm.rb
#
require 'optparse'
Version = "0.1a"
ProgramOptions = Struct.new("ProgramOptions", :hostFile)
def mainProg
options = ProgramOptions.new("#{ENV['HOME']}/hosts.txt")
optparse = OptionParser.new
optparse.on("-h","--hostfile HOSTNAMES","Hostfilename") { |a| options[:hostFile] = a }
begin
optparse.parse(ARGV)
rescue
end
# finally return options
options
end
def readHostFile(fname)
hostArray = Array.new
begin
hf = File.open(fname, "r")
rescue
puts "Unable to open file #{fname}"
return hostArray
end
p hf.class
# foreach line
hf.each_line { |l|
hostArray << l.chomp
}
hf.close
# finally return the hostArray
hostArray
end
def menu(ary)
indx = choice = 0
puts "Host menu. #{Version}"
puts "#{choice} Quit"
while (indx < ary.size) do
choice = choice.succ
puts "#{choice} #{ary[indx]}"
indx = indx.succ
end
end
def getInput
print "Choice: "
STDOUT.flush
begin
choice = STDIN.gets
choice = choice.to_i
rescue
choice = 0
end
return choice
end
opts = mainProg()
#p opts
hl = readHostFile(opts.hostFile)
#p hl
choice = 99
while (choice != 0) do
menu(hl)
choice = getInput()
if (choice > 0 && choice <= hl.size) then
cmd = "ssh -t #{hl[choice-1]}"
begin
rc = system "#{cmd}"
rescue
choice = 0
end
end
end