1 | #!/usr/bin/ruby1.8 -w
|
---|
2 |
|
---|
3 | ###
|
---|
4 | # swconf.rb
|
---|
5 | # Ruby telnet version of the XorPlus range configuration script.
|
---|
6 | ###
|
---|
7 |
|
---|
8 | require 'lb9a.rb'
|
---|
9 | require 'logger'
|
---|
10 | require 'optparse'
|
---|
11 |
|
---|
12 |
|
---|
13 | $options = Hash.new()
|
---|
14 | $optparse = OptionParser.new do |opts|
|
---|
15 | #Banner
|
---|
16 | opts.banner = "Allows configuration of multiple ports on a Quanta running XorPlus.\nUsage: ./swconf.rb [host] [port]"
|
---|
17 | $options[:dbg] = false
|
---|
18 | opts.on('-d','--debug','Enable verbose debugging mode (default: off)') do
|
---|
19 | $options[:dbg] = true
|
---|
20 | end
|
---|
21 | $options[:dr] = false
|
---|
22 | opts.on('-r','--dryrun','Do a dry run (Do not actually send command - default: off)') do
|
---|
23 | $options[:dr] = true
|
---|
24 | end
|
---|
25 | opts.on( '-h', '--help', 'Display this list' ) do
|
---|
26 | puts opts
|
---|
27 | exit
|
---|
28 | end
|
---|
29 | end
|
---|
30 | $host,$port = *$optparse.parse!
|
---|
31 |
|
---|
32 | def main_help()
|
---|
33 | puts "\tclear - clear the screen"
|
---|
34 | puts "\tdelete - delete settings"
|
---|
35 | puts "\texit - exit script"
|
---|
36 | puts "\thelp - display this list"
|
---|
37 | puts "\tiface [eth0|eth1] [IP addr] - Configure management interface address. Address is in CIDR form (e.g 172.16.0.30/16)"
|
---|
38 | puts "\tports [portrange] - Configure VLAN and mode of ports in a list of ports [portrange]"
|
---|
39 | puts "\tsave - Commit changes to memory"
|
---|
40 | puts "\tvlan [VLAN ID] - instantiate a VLAN interface of tag [VLAN ID]"
|
---|
41 | end
|
---|
42 |
|
---|
43 | def ports_help()
|
---|
44 | puts "\tvlan [1..1020]: set native vlan for ports"
|
---|
45 | puts "\tmode [access|trunk]"
|
---|
46 | puts "\tdelete: wipe out port settings"
|
---|
47 | puts "\trange [portrange]: configure new range of ports"
|
---|
48 | puts "\texit: exit from port context"
|
---|
49 | end
|
---|
50 |
|
---|
51 | def prompt(prompt)
|
---|
52 | print prompt
|
---|
53 | ret = gets.chomp
|
---|
54 | return ret
|
---|
55 | end
|
---|
56 |
|
---|
57 | if __FILE__ == $0
|
---|
58 | LOG.debug "host: #{$host} port: #{$port}"
|
---|
59 | @saved = false
|
---|
60 | @changed = false # to true if any configurations are made
|
---|
61 |
|
---|
62 | #initialize session
|
---|
63 | @sn = Session.new($host, $port, $options[:dbg], $options[:dr])
|
---|
64 | @sn.init
|
---|
65 | LOG.debug "Session: #{@sn}"
|
---|
66 |
|
---|
67 | #clear ARGV for gets
|
---|
68 | ARGV.clear
|
---|
69 |
|
---|
70 | #get user input, primary interface
|
---|
71 | resp = prompt("> ").split(' ')
|
---|
72 | rlen = resp.length
|
---|
73 |
|
---|
74 | #CLI loop
|
---|
75 | while (resp[0] != "exit")
|
---|
76 | case resp[0]
|
---|
77 | when "clear" #clear terminal
|
---|
78 | system("clear")
|
---|
79 | when "iface"
|
---|
80 | if rlen == 3
|
---|
81 | iface = resp[1]
|
---|
82 | addr = resp[2]
|
---|
83 | @changed = @sn.set_iface(iface, addr)
|
---|
84 | else
|
---|
85 | puts "\tiface [eth0|eth1] [IP addr/mask]"
|
---|
86 | end
|
---|
87 | when "vlan"
|
---|
88 | if rlen == 2
|
---|
89 | id = resp[1].to_i
|
---|
90 | @changed = @sn.set_vlan(id)
|
---|
91 | else
|
---|
92 | puts "\tvlan [VLAN ID]"
|
---|
93 | end
|
---|
94 | when "ports"
|
---|
95 | if rlen == 2
|
---|
96 | parr = resp[1]
|
---|
97 |
|
---|
98 | ilist = parr.to_ra.to_rng.sort.uniq
|
---|
99 | ctxt = prompt("(#{resp[0]})# ")
|
---|
100 |
|
---|
101 | while ctxt != "exit"
|
---|
102 | # range [portslist] context
|
---|
103 | if ctxt.include? "range"
|
---|
104 | ilist = ctxt.split(' ')[1].to_ra.to_rng.sort.uniq
|
---|
105 | LOG.debug("swconf.rb:ilist:#{ilist}")
|
---|
106 | elsif ctxt.include? "help"
|
---|
107 | ports_help
|
---|
108 | elsif ctxt.include? "clear"
|
---|
109 | system("clear")
|
---|
110 | else
|
---|
111 | @changed = @sn.set_ports(ctxt, ilist)
|
---|
112 | end
|
---|
113 | ctxt = prompt("(#{resp[0]})# ")
|
---|
114 | end
|
---|
115 | else
|
---|
116 | puts "ports [portrange]"
|
---|
117 | puts "[portrange] is a string of ports you want to configure (e.g. \"1..4,7,23\")"
|
---|
118 | end
|
---|
119 | when "delete"
|
---|
120 | if rlen >= 3
|
---|
121 | if resp[1] == "vlan"
|
---|
122 | @changed = @sn.set_vlan(resp[2].to_i, true)
|
---|
123 | elsif resp[1] == "iface"
|
---|
124 | @changed = @sn.set_iface(resp[2], resp[3], true)
|
---|
125 | else
|
---|
126 | puts "swconf.rb:#{resp[1]}:unknown parameter"
|
---|
127 | end
|
---|
128 | else
|
---|
129 | puts "\tdelete iface [eth0|eth1] [address/mask] - delete management port settings"
|
---|
130 | puts "\tdelete vlan [VLAN ID] - delete VLANS"
|
---|
131 | end
|
---|
132 | when "save"
|
---|
133 | @sn.commit
|
---|
134 | @saved = true
|
---|
135 | when "help"
|
---|
136 | main_help
|
---|
137 | else
|
---|
138 | puts "\tUnknown command"
|
---|
139 | end
|
---|
140 |
|
---|
141 | resp.clear
|
---|
142 | resp = prompt("> ").split(' ')
|
---|
143 | rlen = resp.length
|
---|
144 | end
|
---|
145 |
|
---|
146 | if (@changed && !@saved)
|
---|
147 | print "Exit without commit? ([y]/n): "
|
---|
148 | if (gets.chomp == 'n') then @sn.commit end
|
---|
149 | end
|
---|
150 |
|
---|
151 | puts "Logging off"
|
---|
152 | @sn.logoff(@saved)
|
---|
153 | end
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|