Skip to content

Instantly share code, notes, and snippets.

@vranystepan
Created March 16, 2012 06:42
Show Gist options
  • Save vranystepan/2048808 to your computer and use it in GitHub Desktop.
Save vranystepan/2048808 to your computer and use it in GitHub Desktop.
Ruby iLO configurator
require 'optparse'
require 'socket'
require 'openssl'
#functions
def openfile(filename)
lines = Array.new
file = File.open(filename)
file.readlines.each{|line|
lines.push(line)
}
file.close
return lines
end
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: ruby #{File.basename(__FILE__)} -t <target ip address> -i <input file name> [-o <output file name>]"
opts.on( '-t', '--target HOST', 'Target\'s IP address' ) do|h|
options[:host] = h
end
opts.on('-i', '--input FILE', 'Input file name') do |f|
options[:input] = f
end
opts.on('-o', '--output FILE', 'Input file name') do |f|
options[:output] = f
end
opts.on('-u', '--user NAME', 'User name') do |n|
options[:user] = n
end
opts.on('-p', '--password PASSWORD', 'User\'s password') do |p|
options[:password] = p
end
end
begin
optparse.parse!
mandatory = [:host, :input]
missing = mandatory.select{ |param| options[param].nil? }
if not missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts optparse
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end
#load file
unless File.exists?(options[:input]) then
puts "File #{options[:input]} doesn't exist!"
puts optparse
exit 1
end
conf = openfile(options[:input])
output = ""
begin
socket = TCPSocket.new(options[:host], 443)
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
ssl_socket.connect
ssl_socket.print("<?xml version='1.0'?>\r\n")
conf.each{|c|
if c =~ /<LOGIN/
unless options[:user].nil? and options[:password].nil?
c = "<LOGIN USER_LOGIN=\"#{options[:user]}\" PASSWORD=\"#{options[:password]}\">"
end
end
ssl_socket.print(c + "\r\n")
}
while line = ssl_socket.gets
output += line
end
ssl_socket.close
rescue Exception => e
puts "Error, interputted: " + e.message
puts e.backtrace.inspect
end
puts output
unless options[:output].nil?
file = File.new(options[:output], "w")
file.puts output
file.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment