Skip to content

Instantly share code, notes, and snippets.

@crmaxx
Last active February 23, 2016 13:22
Show Gist options
  • Save crmaxx/ea83a085557791f89b1e to your computer and use it in GitHub Desktop.
Save crmaxx/ea83a085557791f89b1e to your computer and use it in GitHub Desktop.
Guacamole config generator, PoC
#!/usr/bin/env ruby
require 'nokogiri'
@host = "10.211.55.18"
@port = "56139"
@name = "#{@host}:#{@port}"
@guacamole_config = ARGV[0]
def remove_config(root)
root.css("config").each(&:remove)
end
def check_config(root)
root.css("config").count.zero?
end
def new_config(root)
config = Nokogiri::XML::Node.new("config", root)
config.set_attribute("name", @name)
config.set_attribute("protocol", 'vnc')
config
end
def add_hostname(config)
param = Nokogiri::XML::Node.new("param", config)
param.set_attribute("name", "hostname")
param.set_attribute("value", @host)
config.add_child(param)
end
def add_port(config)
param = Nokogiri::XML::Node.new("param", config)
param.set_attribute("name", "port")
param.set_attribute("value", @port)
config.add_child(param)
end
def write_config_to_file(root)
File.open(@guacamole_config, 'w') do |file|
file.write("#{root.to_xml(indent: 5, encoding: 'UTF-8').gsub("\n", '').gsub(/\s{2,3}/, '').strip}")
end
end
config = Nokogiri::XML(File.read(@guacamole_config))
fail "Empty XML" unless config.xml?
root = config.root
remove_config(root) unless check_config(root)
config = new_config(root)
add_hostname(config)
add_port(config)
root.add_child(config)
write_config_to_file(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment