I’m learning Ruby, and as an early project, I thought I’d write a little SOAP server for exposing Solaris “kstat” output to the network.
It’s embryonic, but it works. I’d like to do a little more input sanitation, since it’s certainly possible to really hurt your server by requesting a kstat value of “::” (which returns all stats).
Here’s the server code:
require 'soap/rpc/standaloneServer'
class StatServ < SOAP::RPC::StandaloneServer
def initialize(*args)
super
add_method(self, 'kstat', 'stat')
end
def kstat(stat)
stat.gsub!(/[^a-zA-z0-9_:+]/i,'%');
f = IO.popen("kstat -p #{stat}")
return f.readlines
end
end
server = StatServ.new('StatServ,'urn:StatServ','0.0.0.0',8888)
trap('INT') { server.shutdown ; puts "Server shutdown."}
trap('TERM') { server.shutdown ; puts "Server shutdown."}
puts "Starting server..."
server.start
…and the client code:
require 'soap/rpc/driver'
def usage
puts "Usage: #{$0} hostname kstatvalue"
puts " Retrieves a kstat value from a remote machine's StatServ SOAP Server."
end
hostname = ARGV[0]
value = ARGV[1];
if ! value
usage;
exit 1;
end
driver = SOAP::RPC::Driver.new("http://#{hostname}:8888/", 'urn:StatServ')
driver.add_method('kstat', 'stat')
val = driver.kstat("#{value}")
val.each do |s|
puts "#{s}"
end