blob: d551bbba7d194742a87cbe944a8bb8229b958525 [file] [log] [blame]
Steve McIntyrecc297112014-08-11 18:46:58 +01001#! /usr/bin/python
2
3# Copyright 2014 Linaro Limited
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18# MA 02110-1301, USA.
19
20import logging
21import pexpect
22import sys
23import time
24import re
25from common import SwitchDriver
26
27class CiscoSX300(SwitchDriver):
28
29 connection = None
Steve McIntyre366046f2014-08-18 18:57:03 +010030
31 # No extra capabilities for this switch/driver yet
32 _capabilities = [
33 ]
34
Steve McIntyrecc297112014-08-11 18:46:58 +010035 # Regexp of expected hardware information - fail if we don't see
36 # this
Steve McIntyref1699322014-08-19 22:49:43 +010037 _expected_descr_re = re.compile('S.300-\d+')
Steve McIntyrecc297112014-08-11 18:46:58 +010038
Steve McIntyre16f02162014-08-14 17:47:36 +010039 logfile = None
Steve McIntyrecc297112014-08-11 18:46:58 +010040
Steve McIntyre48dc6ae2014-12-23 16:08:19 +000041 def __init__(self, switch_hostname, switch_telnetport=23, debug = False):
42 if debug:
43 logfile = sys.stderr
Steve McIntyrecc297112014-08-11 18:46:58 +010044 self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport)
45
46 ################################
47 ### Switch-level API functions
48 ################################
49
50 # Connect to the switch and log in
Steve McIntyrec1e42b72014-12-22 16:13:08 +000051 def switch_connect(self, username, password, enablepassword):
Steve McIntyrecc297112014-08-11 18:46:58 +010052 logging.debug("Connecting to Switch with: %s" % self.exec_string)
53 self.connection = pexpect.spawn(self.exec_string, logfile = self.logfile)
54 self._login(username, password)
55
Steve McIntyredf9c2c92014-08-12 18:14:49 +010056 # Try to avoid paged output
Steve McIntyre2b4c07b2014-12-22 16:10:04 +000057 self.connection.setwinsize(132, 1000)
Steve McIntyre7ced0732014-08-12 18:07:56 +010058
Steve McIntyrecc297112014-08-11 18:46:58 +010059 # And grab details about the switch. in case we need it
Steve McIntyre366046f2014-08-18 18:57:03 +010060 self._get_systemdata()
Steve McIntyrecc297112014-08-11 18:46:58 +010061
62 # And also validate them - make sure we're driving a switch of
63 # the correct model! Also store the serial number
64 descr_regex = re.compile('System Description:.\s+(\S.*)')
65 sn_regex = re.compile('SN:\s+(\S_)')
66 descr = ""
67
Steve McIntyre366046f2014-08-18 18:57:03 +010068 for line in self._systemdata:
Steve McIntyrecc297112014-08-11 18:46:58 +010069 match = descr_regex.match(line)
70 if match:
71 descr = match.group(1)
72 match = sn_regex.match(line)
73 if match:
74 self.serial_number = match.group(1)
75
Steve McIntyre366046f2014-08-18 18:57:03 +010076 if not self._expected_descr_re.match(descr):
Steve McIntyrecc297112014-08-11 18:46:58 +010077 raise IOError("Switch %s not recognised by this driver: abort" % descr)
78
Steve McIntyre28d34ca2014-08-12 15:40:24 +010079 # Now build a list of our ports, for later sanity checking
Steve McIntyre366046f2014-08-18 18:57:03 +010080 self._ports = self._get_port_names()
81 if len(self._ports) < 4:
Steve McIntyre28d34ca2014-08-12 15:40:24 +010082 raise IOError("Not enough ports detected - problem!")
83
Steve McIntyrecc297112014-08-11 18:46:58 +010084 # Log out of the switch and drop the connection and all state
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010085 def switch_disconnect(self):
Steve McIntyrecc297112014-08-11 18:46:58 +010086 self._logout()
87 logging.debug("Closing connection: %s" % self.connection)
88 self.connection.close(True)
Steve McIntyre11393992014-10-10 15:53:34 +010089 self._ports = []
90 self._prompt_name = ''
91 self._systemdata = []
Steve McIntyrecc297112014-08-11 18:46:58 +010092 del(self)
93
94 # Save the current running config into flash - we want config to
95 # remain across reboots
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010096 def switch_save_running_config(self):
Steve McIntyrecc297112014-08-11 18:46:58 +010097 self._cli("copy running-config startup-config")
98 self.connection.expect("Y/N")
99 self._cli("y")
Steve McIntyre2b4c07b2014-12-22 16:10:04 +0000100 self.connection.expect("succeeded")
Steve McIntyrecc297112014-08-11 18:46:58 +0100101
Steve McIntyre095b4452014-12-19 17:53:43 +0000102 # Restart the switch - we need to reload config to do a
103 # roll-back. Do NOT save running-config first if the switch asks -
104 # we're trying to dump recent changes, not save them.
105 #
106 # This will also implicitly cause a connection to be closed
107 def switch_restart(self):
108 self._cli("reload")
109 index = self.connection.expect(['Are you sure', 'will reset'])
110 if index == 0:
111 self._cli("y") # Yes, continue without saving
112 self.connection.expect("reset the whole")
113
114 # Fall through
115 self._cli("y") # Yes, continue to reset
116 self.connection.close(True)
117
Steve McIntyrecc297112014-08-11 18:46:58 +0100118 ################################
119 ### VLAN API functions
120 ################################
121
122 # Create a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100123 def vlan_create(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100124 logging.debug("Creating VLAN %d" % tag)
125 self._configure()
126 self._cli("vlan database")
127 self._cli("vlan %d" % tag)
128 self._end_configure()
129
130 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100131 vlans = self.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100132 for vlan in vlans:
133 if vlan == tag:
134 return
135 raise IOError("Failed to create VLAN %d" % tag)
136
137 # Destroy a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100138 def vlan_destroy(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100139 logging.debug("Destroying VLAN %d" % tag)
140 self._configure()
141 self._cli("no vlan %d" % tag)
142 self._end_configure()
143
144 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100145 vlans = self.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100146 for vlan in vlans:
147 if vlan == tag:
148 raise IOError("Failed to destroy VLAN %d" % tag)
149
150 # Set the name of a VLAN
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100151 def vlan_set_name(self, tag, name):
Steve McIntyrecc297112014-08-11 18:46:58 +0100152 logging.debug("Setting name of VLAN %d to %s" % (tag, name))
153 self._configure()
154 self._cli("vlan %d" % tag)
155 self._cli("interface vlan %d" % tag)
156 self._cli("name %s" % name)
157 self._end_configure()
158
159 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100160 read_name = self.vlan_get_name(tag)
Steve McIntyrecc297112014-08-11 18:46:58 +0100161 if read_name != name:
162 raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")"
163 % (tag, read_name, name))
164
165 # Get a list of the VLAN tags currently registered on the switch
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100166 def vlan_get_list(self):
Steve McIntyrecc297112014-08-11 18:46:58 +0100167 logging.debug("Grabbing list of VLANs")
168 vlans = []
169
170 regex = re.compile('^ *(\d+).*(D|S|G|R)')
171
172 self._cli("show vlan")
173 for line in self._read_paged_output():
174 match = regex.match(line)
175 if match:
176 vlans.append(int(match.group(1)))
177 return vlans
178
179 # For a given VLAN tag, ask the switch what the associated name is
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100180 def vlan_get_name(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100181 logging.debug("Grabbing the name of VLAN %d" % tag)
182 name = None
183 regex = re.compile('^ *\d+\s+(\S+).*(D|S|G|R)')
184 self._cli("show vlan tag %d" % tag)
185 for line in self._read_paged_output():
186 match = regex.match(line)
187 if match:
188 name = match.group(1)
189 name.strip()
190 return name
191
192
193 ################################
194 ### Port API functions
195 ################################
196
Steve McIntyre9936d002014-10-01 15:54:10 +0100197 # Set the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100198 def port_set_mode(self, port, mode):
Steve McIntyrecc297112014-08-11 18:46:58 +0100199 logging.debug("Setting port %s to %s" % (port, mode))
200 if not self._is_port_mode_valid(mode):
201 raise IndexError("Port mode %s is not allowed" % mode)
202 if not self._is_port_name_valid(port):
203 raise IndexError("Port name %s not recognised" % port)
204 self._configure()
205 self._cli("interface %s" % port)
206 self._cli("switchport mode %s" % mode)
207 self._end_configure()
208
209 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100210 read_mode = self.port_get_mode(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100211 if read_mode != mode:
212 raise IOError("Failed to set mode for port %s" % port)
213
214
Steve McIntyre9936d002014-10-01 15:54:10 +0100215 # Get the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100216 def port_get_mode(self, port):
Steve McIntyrecc297112014-08-11 18:46:58 +0100217 logging.debug("Getting mode of port %s" % port)
218 mode = ''
219 if not self._is_port_name_valid(port):
220 raise IndexError("Port name %s not recognised" % port)
221 regex = re.compile('Port Mode: (\S+)')
222 self._cli("show interfaces switchport %s" % port)
223 for line in self._read_paged_output():
224 match = regex.match(line)
225 if match:
226 mode = match.group(1)
227 return mode.lower()
228
Steve McIntyre9936d002014-10-01 15:54:10 +0100229 # Set an access port to be in a specified VLAN (tag)
230 def port_set_access_vlan(self, port, tag):
231 logging.debug("Setting access port %s to VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100232 if not self._is_port_name_valid(port):
233 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100234 if not (self.port_get_mode(port) == "access"):
235 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100236
Steve McIntyre9936d002014-10-01 15:54:10 +0100237 self._configure()
238 self._cli("interface %s" % port)
239 self._cli("switchport access vlan %d" % tag)
240 self._end_configure()
Steve McIntyrecc297112014-08-11 18:46:58 +0100241
Steve McIntyre9936d002014-10-01 15:54:10 +0100242 # Validate things worked
243 read_vlan = int(self.port_get_access_vlan(port))
Steve McIntyrecc297112014-08-11 18:46:58 +0100244 if read_vlan != tag:
Steve McIntyre9936d002014-10-01 15:54:10 +0100245 raise IOError("Failed to move access port %s to VLAN %d - got VLAN %d instead"
Steve McIntyrecc297112014-08-11 18:46:58 +0100246 % (port, tag, read_vlan))
247
Steve McIntyrecc297112014-08-11 18:46:58 +0100248 # Add a trunk port to a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100249 def port_add_trunk_to_vlan(self, port, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100250 logging.debug("Adding trunk port %s to VLAN %d" % (port, tag))
251 if not self._is_port_name_valid(port):
252 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100253 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyrecc297112014-08-11 18:46:58 +0100254 raise IndexError("Port %s not in trunk mode" % port)
255 self._configure()
256 self._cli("interface %s" % port)
257 self._cli("switchport trunk allowed vlan add %d" % tag)
258 self._end_configure()
259
260 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100261 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100262 for vlan in read_vlans:
263 if vlan == tag:
264 return
Steve McIntyre5b3ce212014-08-15 13:10:41 +0100265 raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100266
267 # Remove a trunk port from a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100268 def port_remove_trunk_from_vlan(self, port, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100269 logging.debug("Removing trunk port %s from VLAN %d" % (port, tag))
270 if not self._is_port_name_valid(port):
271 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100272 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyrecc297112014-08-11 18:46:58 +0100273 raise IndexError("Port %s not in trunk mode" % port)
274 self._configure()
275 self._cli("interface %s" % port)
276 self._cli("switchport trunk allowed vlan remove %d" % tag)
277 self._end_configure()
278
279 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100280 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100281 for vlan in read_vlans:
282 if vlan == tag:
Steve McIntyre5b3ce212014-08-15 13:10:41 +0100283 raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100284
Steve McIntyre9936d002014-10-01 15:54:10 +0100285 # Get the configured VLAN tag for an access port (tag)
286 def port_get_access_vlan(self, port):
287 logging.debug("Getting VLAN for access port %s" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100288 vlan = 1
289 if not self._is_port_name_valid(port):
290 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100291 if not (self.port_get_mode(port) == "access"):
292 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100293 regex = re.compile('(\d+)\s+\S+\s+Untagged\s+Static')
294 self._cli("show interfaces switchport %s" % port)
295 for line in self._read_paged_output():
296 match = regex.match(line)
297 if match:
298 vlan = match.group(1)
299 return int(vlan)
300
301 # Get the list of configured VLAN tags for a trunk port
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100302 def port_get_trunk_vlan_list(self, port):
Steve McIntyrecc297112014-08-11 18:46:58 +0100303 logging.debug("Getting VLANs for trunk port %s" % port)
304 vlans = [ ]
305 if not self._is_port_name_valid(port):
306 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100307 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyree5ea20a2014-10-20 10:39:31 +0100308 raise IndexError("Port %s not in trunk mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100309 regex = re.compile('(\d+)\s+\S+\s+(Tagged|Untagged)\s+Static')
310 self._cli("show interfaces switchport %s" % port)
311 for line in self._read_paged_output():
312 match = regex.match(line)
313 if match:
314 vlans.append (int(match.group(1)))
315 return vlans
316
317 ################################
318 ### Internal functions
319 ################################
320
321 def _login(self, username, password):
322 logging.debug("attempting login with username %s, password %s" % (username, password))
323 self._cli("")
324 self.connection.expect("User Name:")
325 self._cli("%s" % username)
326 self.connection.expect("Password:")
327 self._cli("%s" % password, False)
Steve McIntyreca6c5a32014-12-23 15:34:29 +0000328 self.connection.expect("\*\*")
Steve McIntyrecc297112014-08-11 18:46:58 +0100329 while True:
Steve McIntyreca6c5a32014-12-23 15:34:29 +0000330 index = self.connection.expect(['User Name:', 'authentication failed', r'(.*)#', 'Password:', '.+'])
Steve McIntyrecc297112014-08-11 18:46:58 +0100331 if index == 0 or index == 1: # Failed to log in!
332 logging.error("Login failure: %s\n" % self.connection.match)
333 raise IOError
334 elif index == 2:
Steve McIntyre366046f2014-08-18 18:57:03 +0100335 self._prompt_name = self.connection.match.group(1).strip()
Steve McIntyreca6c5a32014-12-23 15:34:29 +0000336 logging.debug("Got prompt name %s" % self._prompt_name)
Steve McIntyrecc297112014-08-11 18:46:58 +0100337 return 0
Steve McIntyreca6c5a32014-12-23 15:34:29 +0000338 elif index == 3 or index == 4:
339 self._cli("", False)
Steve McIntyrecc297112014-08-11 18:46:58 +0100340
341 def _logout(self):
342 logging.debug("Logging out")
343 self._cli("exit", False)
344
Steve McIntyrecc297112014-08-11 18:46:58 +0100345 def _configure(self):
346 self._cli("configure terminal")
347
348 def _end_configure(self):
349 self._cli("end")
350
351 def _read_paged_output(self):
352 buf = []
Steve McIntyre366046f2014-08-18 18:57:03 +0100353 prompt = self._prompt_name + '#'
Steve McIntyrecc297112014-08-11 18:46:58 +0100354 while True:
355 index = self.connection.expect(['\x1b\[0mMore:.*<return>.*$', prompt])
356 if index == 0: # More: <space>
357 for line in self.connection.before.split('\r\n'):
358 buf.append(line.strip())
359 self._cli(' ', False)
360 elif index == 1: # Back to a prompt, says output is finished
361 break
362
363 for line in self.connection.before.split('\r\n'):
364 buf.append(line.strip())
365
366 return buf
367
368 def _get_port_names(self):
369 logging.debug("Grabbing list of ports")
370 interfaces = []
371
372 # Use "Up" or "Down" to only identify lines in the output that
373 # match interfaces that exist
374 regex = re.compile('^(\w+).*(Up|Down)')
375
376 self._cli("show interfaces status detailed")
377 for line in self._read_paged_output():
378 match = regex.match(line)
379 if match:
380 interfaces.append(match.group(1))
381 return interfaces
382
Steve McIntyrecc297112014-08-11 18:46:58 +0100383 def _show_config(self):
384 logging.debug("Grabbing config")
385 self._cli("show running-config")
386 return self._read_paged_output()
387
388 def _show_clock(self):
389 logging.debug("Grabbing time")
390 self._cli("show clock")
391 return self._read_paged_output()
392
Steve McIntyrecc297112014-08-11 18:46:58 +0100393 def _get_systemdata(self):
Steve McIntyrecc297112014-08-11 18:46:58 +0100394
Steve McIntyreffb9b5a2014-10-10 16:31:58 +0100395 self._systemdata = []
396
Steve McIntyre28d34ca2014-08-12 15:40:24 +0100397 logging.debug("Grabbing system data")
Steve McIntyrecc297112014-08-11 18:46:58 +0100398 self._cli("show system")
399 for line in self._read_paged_output():
Steve McIntyre366046f2014-08-18 18:57:03 +0100400 self._systemdata.append(line)
Steve McIntyre28d34ca2014-08-12 15:40:24 +0100401
402 logging.debug("Grabbing system sw and hw versions")
403 self._cli("show version")
404 for line in self._read_paged_output():
Steve McIntyre366046f2014-08-18 18:57:03 +0100405 self._systemdata.append(line)
Steve McIntyrecc297112014-08-11 18:46:58 +0100406
Steve McIntyre16f02162014-08-14 17:47:36 +0100407 ######################################
408 # Internal port access helper methods
409 ######################################
410 # N.B. No parameter checking here, for speed reasons - if you're
411 # calling this internal API then you should already have validated
412 # things yourself! Equally, no post-set checks in here - do that
413 # at the higher level.
414 ######################################
415
Steve McIntyrecc297112014-08-11 18:46:58 +0100416 # Wrapper around connection.send - by default, expect() the same
417 # text we've sent, to remove it from the output from the
418 # switch. For the few cases where we don't need that, override
419 # this using echo=False.
420 # Horrible, but seems to work.
421 def _cli(self, text, echo=True):
422 self.connection.send(text + '\r')
423 if echo:
424 self.connection.expect(text)
425
426if __name__ == "__main__":
Steve McIntyref1699322014-08-19 22:49:43 +0100427# p = CiscoSX300('10.172.2.52', 23)
Steve McIntyre48dc6ae2014-12-23 16:08:19 +0000428
429 import optparse
430
431 switch = 'vlandswitch02'
432 parser = optparse.OptionParser()
433 parser.add_option("--switch",
434 dest = "switch",
435 action = "store",
436 nargs = 1,
437 type = "string",
438 help = "specify switch to connect to for testing",
439 metavar = "<switch>")
440 (opts, args) = parser.parse_args()
441 if opts.switch:
442 switch = opts.switch
443
444 p = CiscoSX300(switch, 23, debug = True)
Steve McIntyreaea995c2014-12-22 17:17:38 +0000445 p.switch_connect('cisco', 'cisco', None)
Steve McIntyrecc297112014-08-11 18:46:58 +0100446 #buf = p._show_clock()
447 #print "%s" % buf
448 #buf = p._show_config()
449 #p._dump_list(buf)
450
Steve McIntyre366046f2014-08-18 18:57:03 +0100451 print "System data:"
452 p._dump_list(p._systemdata)
Steve McIntyrecc297112014-08-11 18:46:58 +0100453
454 print "Creating VLANs for testing:"
455 for i in [ 2, 3, 4, 5, 20 ]:
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100456 p.vlan_create(i)
457 p.vlan_set_name(i, "test%d" % i)
Steve McIntyrecc297112014-08-11 18:46:58 +0100458 print " %d (test%d)" % (i, i)
459
460 #print "And dump config\n"
461 #buf = p._show_config()
462 #print "%s" % buf
463
464 #print "Destroying VLAN 2\n"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100465 #p.vlan_destroy(2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100466
467 #print "And dump config\n"
468 #buf = p._show_config()
469 #print "%s" % buf
470
471 #print "Port names are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100472 #buf = p.switch_get_port_names()
Steve McIntyrecc297112014-08-11 18:46:58 +0100473 #p._dump_list(buf)
474
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100475 #buf = p.vlan_get_name(25)
Steve McIntyrecc297112014-08-11 18:46:58 +0100476 #print "VLAN with tag 25 is called \"%s\"" % buf
477
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100478 #p.vlan_set_name(35, "foo")
Steve McIntyrecc297112014-08-11 18:46:58 +0100479 #print "VLAN with tag 35 is called \"foo\""
480
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100481 #buf = p.port_get_mode("fa12")
Steve McIntyrecc297112014-08-11 18:46:58 +0100482 #print "Port fa12 is in %s mode" % buf
483
Steve McIntyre9936d002014-10-01 15:54:10 +0100484 # Test access stuff
485 print "Set fa6 to access mode"
486 p.port_set_mode("fa6", "access")
Steve McIntyre16f02162014-08-14 17:47:36 +0100487 print "Move fa6 to VLAN 2"
Steve McIntyre9936d002014-10-01 15:54:10 +0100488 p.port_set_access_vlan("fa6", 2)
489 buf = p.port_get_access_vlan("fa6")
Steve McIntyrecc297112014-08-11 18:46:58 +0100490 print "Read from switch: fa6 is on VLAN %s" % buf
Steve McIntyre16f02162014-08-14 17:47:36 +0100491 print "Move fa6 back to default VLAN 1"
Steve McIntyre9936d002014-10-01 15:54:10 +0100492 p.port_set_access_vlan("fa6", 1)
Steve McIntyrecc297112014-08-11 18:46:58 +0100493 #print "And move fa6 back to a trunk port"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100494 #p.port_set_mode("fa6", "trunk")
495 #buf = p.port_get_mode("fa6")
Steve McIntyrecc297112014-08-11 18:46:58 +0100496 #print "Port fa6 is in %s mode" % buf
497
498 # Test trunk stuff
499 print "Set gi2 to trunk mode"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100500 p.port_set_mode("gi2", "trunk")
Steve McIntyrecc297112014-08-11 18:46:58 +0100501 print "Add gi2 to VLAN 2"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100502 p.port_add_trunk_to_vlan("gi2", 2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100503 print "Add gi2 to VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100504 p.port_add_trunk_to_vlan("gi2", 3)
Steve McIntyrecc297112014-08-11 18:46:58 +0100505 print "Add gi2 to VLAN 4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100506 p.port_add_trunk_to_vlan("gi2", 4)
Steve McIntyrecc297112014-08-11 18:46:58 +0100507 print "Read from switch: which VLANs is gi2 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100508 buf = p.port_get_trunk_vlan_list("gi2")
Steve McIntyrecc297112014-08-11 18:46:58 +0100509 p._dump_list(buf)
510
Steve McIntyre366046f2014-08-18 18:57:03 +0100511 print "Remove gi2 from VLANs 3,3,4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100512 p.port_remove_trunk_from_vlan("gi2", 3)
513 p.port_remove_trunk_from_vlan("gi2", 3)
514 p.port_remove_trunk_from_vlan("gi2", 4)
Steve McIntyrecc297112014-08-11 18:46:58 +0100515 print "Read from switch: which VLANs is gi2 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100516 buf = p.port_get_trunk_vlan_list("gi2")
Steve McIntyrecc297112014-08-11 18:46:58 +0100517 p._dump_list(buf)
518
519 # print "Adding lots of ports to VLANs"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100520 # p.port_add_trunk_to_vlan("fa1", 2)
521 # p.port_add_trunk_to_vlan("fa3", 2)
522 # p.port_add_trunk_to_vlan("fa5", 2)
523 # p.port_add_trunk_to_vlan("fa7", 2)
524 # p.port_add_trunk_to_vlan("fa9", 2)
525 # p.port_add_trunk_to_vlan("fa11", 2)
526 # p.port_add_trunk_to_vlan("fa13", 2)
527 # p.port_add_trunk_to_vlan("fa15", 2)
528 # p.port_add_trunk_to_vlan("fa17", 2)
529 # p.port_add_trunk_to_vlan("fa19", 2)
530 # p.port_add_trunk_to_vlan("fa21", 2)
531 # p.port_add_trunk_to_vlan("fa23", 2)
532 # p.port_add_trunk_to_vlan("gi4", 2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100533
534 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100535 buf = p.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100536 p._dump_list(buf)
537
Steve McIntyre7460d972014-12-23 14:45:30 +0000538# print 'Restarting switch, to explicitly reset config'
539# p.switch_restart()
Steve McIntyrecc297112014-08-11 18:46:58 +0100540
Steve McIntyrebf5dc882014-12-19 17:54:58 +0000541# p.switch_save_running_config()
Steve McIntyrecc297112014-08-11 18:46:58 +0100542# p._show_config()
543