Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created March 7, 2011 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/858048 to your computer and use it in GitHub Desktop.
Save havenwood/858048 to your computer and use it in GitHub Desktop.
Rubyish switches config
require 'sinatra'
Switch = Struct.new(:vendor, :model, :ports)
Ports = Struct.new(:first, :second)
get '/' do
erb :select_switch
end
post '/' do
case params[:switch]
when "Cisco"
@ports = Ports.new('GigabitEthernet 0/1', 'GigabitEthernet 0/2')
@switch = Switch.new('Cisco', '3560G', @ports)
erb :select_port
when "Stratix"
@ports = Ports.new('TenGigabitEthernet 0/1', 'TenGigabitEthernet 0/2')
@switch = Switch.new('Over', '9000', @ports)
erb :select_port
else
erb :select_switch
end
end
post '/results' do
@port = params[:port]
if @port.nil?
erb :select_switch
else
erb :get_results
end
end
class Log
def self.entry
File.open('tmp/swcfg-log.txt', 'a') { |log| log.puts "#{Time.now.ctime}" }
end
end
__END__
@@ layout
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>swcfg.rb</title>
<style type="text/css">
html, body, form, fieldset, legend { margin: 5; padding: 5; }
body { font: 16px/18px helvetica; padding: 20px; color: #444; }
</style>
</head>
<body>
<%= yield %>
</body>
</html>
@@ select_switch
<form action="/" method="post">
<fieldset>
<legend>Select a Switch</legend>
<p><input type="radio" name="switch" value="Cisco">Cisco</p>
<p><input type="radio" name="switch" value="Stratix">Stratix</p>
<p><input type="submit" value="Select"></p>
</fieldset>
</form>
@@ select_port
<form action="/results" method="post">
<fieldset>
<legend>Select a Port</legend>
<p>Vendor: <%= @switch.vendor %></p>
<p>Model: <%= @switch.model %></p>
<% @switch.ports.each do |key, port| %>
<p><input type="radio" name="port" value="<%= port %>"><%= port %></p>
<% end %>
<p><input type="submit" value="Generate Configuration"></p>
</fieldset>
</form>
@@ get_results
<fieldset>
<legend>Configuration</legend>
<p>You chose: <%= @port %></p>
<p>Configure the port configuration however the port configuration looks...</p>
<p>And transaction logged.</p>
<% Log.entry %>
<p><a href="/"><button>Start Over</button></a></p>
</fieldset>
@havenwood
Copy link
Author

ruby swcfg.rb # results in a log entry being made in log.txt and:

A Xandu 90210 was configured:
Port 0: GigabitEthernet 0/1
Port 1: GigabitEthernet 0/2

A KAPRO 2000 was configured:
Port 0: TenGigabitEthernet 1/1
Port 1: tge0/1/1

@havenwood
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment