blob: 72c7d7dd28c677ea32a61bacfb0af2aa7d764b07 [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
39 logfile = sys.stderr
Steve McIntyre16f02162014-08-14 17:47:36 +010040 logfile = None
Steve McIntyrecc297112014-08-11 18:46:58 +010041
42 def __init__(self, switch_hostname, switch_telnetport=23):
43 self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport)
44
45 ################################
46 ### Switch-level API functions
47 ################################
48
49 # Connect to the switch and log in
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010050 def switch_connect(self, username, password):
Steve McIntyrecc297112014-08-11 18:46:58 +010051 logging.debug("Connecting to Switch with: %s" % self.exec_string)
52 self.connection = pexpect.spawn(self.exec_string, logfile = self.logfile)
53 self._login(username, password)
54
Steve McIntyredf9c2c92014-08-12 18:14:49 +010055 # Try to avoid paged output
Steve McIntyre7ced0732014-08-12 18:07:56 +010056 self.connection.setwinsize(132,1000)
57
Steve McIntyrecc297112014-08-11 18:46:58 +010058 # And grab details about the switch. in case we need it
Steve McIntyre366046f2014-08-18 18:57:03 +010059 self._get_systemdata()
Steve McIntyrecc297112014-08-11 18:46:58 +010060
61 # And also validate them - make sure we're driving a switch of
62 # the correct model! Also store the serial number
63 descr_regex = re.compile('System Description:.\s+(\S.*)')
64 sn_regex = re.compile('SN:\s+(\S_)')
65 descr = ""
66
Steve McIntyre366046f2014-08-18 18:57:03 +010067 for line in self._systemdata:
Steve McIntyrecc297112014-08-11 18:46:58 +010068 match = descr_regex.match(line)
69 if match:
70 descr = match.group(1)
71 match = sn_regex.match(line)
72 if match:
73 self.serial_number = match.group(1)
74
Steve McIntyre366046f2014-08-18 18:57:03 +010075 if not self._expected_descr_re.match(descr):
Steve McIntyrecc297112014-08-11 18:46:58 +010076 raise IOError("Switch %s not recognised by this driver: abort" % descr)
77
Steve McIntyre28d34ca2014-08-12 15:40:24 +010078 # Now build a list of our ports, for later sanity checking
Steve McIntyre366046f2014-08-18 18:57:03 +010079 self._ports = self._get_port_names()
80 if len(self._ports) < 4:
Steve McIntyre28d34ca2014-08-12 15:40:24 +010081 raise IOError("Not enough ports detected - problem!")
82
Steve McIntyrecc297112014-08-11 18:46:58 +010083 # Log out of the switch and drop the connection and all state
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010084 def switch_disconnect(self):
Steve McIntyrecc297112014-08-11 18:46:58 +010085 self._logout()
86 logging.debug("Closing connection: %s" % self.connection)
87 self.connection.close(True)
88 del(self)
89
90 # Save the current running config into flash - we want config to
91 # remain across reboots
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010092 def switch_save_running_config(self):
Steve McIntyrecc297112014-08-11 18:46:58 +010093 self._cli("copy running-config startup-config")
94 self.connection.expect("Y/N")
95 self._cli("y")
96 self.connection.expect("Copy succeeded")
97
Steve McIntyrecc297112014-08-11 18:46:58 +010098 ################################
99 ### VLAN API functions
100 ################################
101
102 # Create a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100103 def vlan_create(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100104 logging.debug("Creating VLAN %d" % tag)
105 self._configure()
106 self._cli("vlan database")
107 self._cli("vlan %d" % tag)
108 self._end_configure()
109
110 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100111 vlans = self.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100112 for vlan in vlans:
113 if vlan == tag:
114 return
115 raise IOError("Failed to create VLAN %d" % tag)
116
117 # Destroy a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100118 def vlan_destroy(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100119 logging.debug("Destroying VLAN %d" % tag)
120 self._configure()
121 self._cli("no vlan %d" % tag)
122 self._end_configure()
123
124 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100125 vlans = self.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100126 for vlan in vlans:
127 if vlan == tag:
128 raise IOError("Failed to destroy VLAN %d" % tag)
129
130 # Set the name of a VLAN
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100131 def vlan_set_name(self, tag, name):
Steve McIntyrecc297112014-08-11 18:46:58 +0100132 logging.debug("Setting name of VLAN %d to %s" % (tag, name))
133 self._configure()
134 self._cli("vlan %d" % tag)
135 self._cli("interface vlan %d" % tag)
136 self._cli("name %s" % name)
137 self._end_configure()
138
139 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100140 read_name = self.vlan_get_name(tag)
Steve McIntyrecc297112014-08-11 18:46:58 +0100141 if read_name != name:
142 raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")"
143 % (tag, read_name, name))
144
145 # Get a list of the VLAN tags currently registered on the switch
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100146 def vlan_get_list(self):
Steve McIntyrecc297112014-08-11 18:46:58 +0100147 logging.debug("Grabbing list of VLANs")
148 vlans = []
149
150 regex = re.compile('^ *(\d+).*(D|S|G|R)')
151
152 self._cli("show vlan")
153 for line in self._read_paged_output():
154 match = regex.match(line)
155 if match:
156 vlans.append(int(match.group(1)))
157 return vlans
158
159 # For a given VLAN tag, ask the switch what the associated name is
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100160 def vlan_get_name(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100161 logging.debug("Grabbing the name of VLAN %d" % tag)
162 name = None
163 regex = re.compile('^ *\d+\s+(\S+).*(D|S|G|R)')
164 self._cli("show vlan tag %d" % tag)
165 for line in self._read_paged_output():
166 match = regex.match(line)
167 if match:
168 name = match.group(1)
169 name.strip()
170 return name
171
172
173 ################################
174 ### Port API functions
175 ################################
176
Steve McIntyre9936d002014-10-01 15:54:10 +0100177 # Set the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100178 def port_set_mode(self, port, mode):
Steve McIntyrecc297112014-08-11 18:46:58 +0100179 logging.debug("Setting port %s to %s" % (port, mode))
180 if not self._is_port_mode_valid(mode):
181 raise IndexError("Port mode %s is not allowed" % mode)
182 if not self._is_port_name_valid(port):
183 raise IndexError("Port name %s not recognised" % port)
184 self._configure()
185 self._cli("interface %s" % port)
186 self._cli("switchport mode %s" % mode)
187 self._end_configure()
188
189 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100190 read_mode = self.port_get_mode(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100191 if read_mode != mode:
192 raise IOError("Failed to set mode for port %s" % port)
193
194
Steve McIntyre9936d002014-10-01 15:54:10 +0100195 # Get the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100196 def port_get_mode(self, port):
Steve McIntyrecc297112014-08-11 18:46:58 +0100197 logging.debug("Getting mode of port %s" % port)
198 mode = ''
199 if not self._is_port_name_valid(port):
200 raise IndexError("Port name %s not recognised" % port)
201 regex = re.compile('Port Mode: (\S+)')
202 self._cli("show interfaces switchport %s" % port)
203 for line in self._read_paged_output():
204 match = regex.match(line)
205 if match:
206 mode = match.group(1)
207 return mode.lower()
208
Steve McIntyre9936d002014-10-01 15:54:10 +0100209 # Set an access port to be in a specified VLAN (tag)
210 def port_set_access_vlan(self, port, tag):
211 logging.debug("Setting access port %s to VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100212 if not self._is_port_name_valid(port):
213 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100214 if not (self.port_get_mode(port) == "access"):
215 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100216
Steve McIntyre9936d002014-10-01 15:54:10 +0100217 self._configure()
218 self._cli("interface %s" % port)
219 self._cli("switchport access vlan %d" % tag)
220 self._end_configure()
Steve McIntyrecc297112014-08-11 18:46:58 +0100221
Steve McIntyre9936d002014-10-01 15:54:10 +0100222 # Validate things worked
223 read_vlan = int(self.port_get_access_vlan(port))
Steve McIntyrecc297112014-08-11 18:46:58 +0100224 if read_vlan != tag:
Steve McIntyre9936d002014-10-01 15:54:10 +0100225 raise IOError("Failed to move access port %s to VLAN %d - got VLAN %d instead"
Steve McIntyrecc297112014-08-11 18:46:58 +0100226 % (port, tag, read_vlan))
227
Steve McIntyrecc297112014-08-11 18:46:58 +0100228 # Add a trunk port to a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100229 def port_add_trunk_to_vlan(self, port, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100230 logging.debug("Adding trunk port %s to VLAN %d" % (port, tag))
231 if not self._is_port_name_valid(port):
232 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100233 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyrecc297112014-08-11 18:46:58 +0100234 raise IndexError("Port %s not in trunk mode" % port)
235 self._configure()
236 self._cli("interface %s" % port)
237 self._cli("switchport trunk allowed vlan add %d" % tag)
238 self._end_configure()
239
240 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100241 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100242 for vlan in read_vlans:
243 if vlan == tag:
244 return
Steve McIntyre5b3ce212014-08-15 13:10:41 +0100245 raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100246
247 # Remove a trunk port from a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100248 def port_remove_trunk_from_vlan(self, port, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100249 logging.debug("Removing trunk port %s from VLAN %d" % (port, tag))
250 if not self._is_port_name_valid(port):
251 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100252 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyrecc297112014-08-11 18:46:58 +0100253 raise IndexError("Port %s not in trunk mode" % port)
254 self._configure()
255 self._cli("interface %s" % port)
256 self._cli("switchport trunk allowed vlan remove %d" % tag)
257 self._end_configure()
258
259 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100260 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100261 for vlan in read_vlans:
262 if vlan == tag:
Steve McIntyre5b3ce212014-08-15 13:10:41 +0100263 raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100264
Steve McIntyre9936d002014-10-01 15:54:10 +0100265 # Get the configured VLAN tag for an access port (tag)
266 def port_get_access_vlan(self, port):
267 logging.debug("Getting VLAN for access port %s" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100268 vlan = 1
269 if not self._is_port_name_valid(port):
270 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100271 if not (self.port_get_mode(port) == "access"):
272 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100273 regex = re.compile('(\d+)\s+\S+\s+Untagged\s+Static')
274 self._cli("show interfaces switchport %s" % port)
275 for line in self._read_paged_output():
276 match = regex.match(line)
277 if match:
278 vlan = match.group(1)
279 return int(vlan)
280
281 # Get the list of configured VLAN tags for a trunk port
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100282 def port_get_trunk_vlan_list(self, port):
Steve McIntyrecc297112014-08-11 18:46:58 +0100283 logging.debug("Getting VLANs for trunk port %s" % port)
284 vlans = [ ]
285 if not self._is_port_name_valid(port):
286 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100287 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyre9936d002014-10-01 15:54:10 +0100288 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100289 regex = re.compile('(\d+)\s+\S+\s+(Tagged|Untagged)\s+Static')
290 self._cli("show interfaces switchport %s" % port)
291 for line in self._read_paged_output():
292 match = regex.match(line)
293 if match:
294 vlans.append (int(match.group(1)))
295 return vlans
296
297 ################################
298 ### Internal functions
299 ################################
300
301 def _login(self, username, password):
302 logging.debug("attempting login with username %s, password %s" % (username, password))
303 self._cli("")
304 self.connection.expect("User Name:")
305 self._cli("%s" % username)
306 self.connection.expect("Password:")
307 self._cli("%s" % password, False)
308 while True:
309 index = self.connection.expect(['User Name:', 'authentication failed', r'(.*)#', '.*'])
310 if index == 0 or index == 1: # Failed to log in!
311 logging.error("Login failure: %s\n" % self.connection.match)
312 raise IOError
313 elif index == 2:
Steve McIntyre366046f2014-08-18 18:57:03 +0100314 self._prompt_name = self.connection.match.group(1).strip()
Steve McIntyrecc297112014-08-11 18:46:58 +0100315 return 0
316
317 def _logout(self):
318 logging.debug("Logging out")
319 self._cli("exit", False)
320
Steve McIntyrecc297112014-08-11 18:46:58 +0100321 def _configure(self):
322 self._cli("configure terminal")
323
324 def _end_configure(self):
325 self._cli("end")
326
327 def _read_paged_output(self):
328 buf = []
Steve McIntyre366046f2014-08-18 18:57:03 +0100329 prompt = self._prompt_name + '#'
Steve McIntyrecc297112014-08-11 18:46:58 +0100330 while True:
331 index = self.connection.expect(['\x1b\[0mMore:.*<return>.*$', prompt])
332 if index == 0: # More: <space>
333 for line in self.connection.before.split('\r\n'):
334 buf.append(line.strip())
335 self._cli(' ', False)
336 elif index == 1: # Back to a prompt, says output is finished
337 break
338
339 for line in self.connection.before.split('\r\n'):
340 buf.append(line.strip())
341
342 return buf
343
344 def _get_port_names(self):
345 logging.debug("Grabbing list of ports")
346 interfaces = []
347
348 # Use "Up" or "Down" to only identify lines in the output that
349 # match interfaces that exist
350 regex = re.compile('^(\w+).*(Up|Down)')
351
352 self._cli("show interfaces status detailed")
353 for line in self._read_paged_output():
354 match = regex.match(line)
355 if match:
356 interfaces.append(match.group(1))
357 return interfaces
358
Steve McIntyrecc297112014-08-11 18:46:58 +0100359 def _show_config(self):
360 logging.debug("Grabbing config")
361 self._cli("show running-config")
362 return self._read_paged_output()
363
364 def _show_clock(self):
365 logging.debug("Grabbing time")
366 self._cli("show clock")
367 return self._read_paged_output()
368
Steve McIntyrecc297112014-08-11 18:46:58 +0100369 def _get_systemdata(self):
Steve McIntyrecc297112014-08-11 18:46:58 +0100370
Steve McIntyre28d34ca2014-08-12 15:40:24 +0100371 logging.debug("Grabbing system data")
Steve McIntyrecc297112014-08-11 18:46:58 +0100372 self._cli("show system")
373 for line in self._read_paged_output():
Steve McIntyre366046f2014-08-18 18:57:03 +0100374 self._systemdata.append(line)
Steve McIntyre28d34ca2014-08-12 15:40:24 +0100375
376 logging.debug("Grabbing system sw and hw versions")
377 self._cli("show version")
378 for line in self._read_paged_output():
Steve McIntyre366046f2014-08-18 18:57:03 +0100379 self._systemdata.append(line)
Steve McIntyrecc297112014-08-11 18:46:58 +0100380
Steve McIntyre16f02162014-08-14 17:47:36 +0100381 ######################################
382 # Internal port access helper methods
383 ######################################
384 # N.B. No parameter checking here, for speed reasons - if you're
385 # calling this internal API then you should already have validated
386 # things yourself! Equally, no post-set checks in here - do that
387 # at the higher level.
388 ######################################
389
Steve McIntyrecc297112014-08-11 18:46:58 +0100390 # Wrapper around connection.send - by default, expect() the same
391 # text we've sent, to remove it from the output from the
392 # switch. For the few cases where we don't need that, override
393 # this using echo=False.
394 # Horrible, but seems to work.
395 def _cli(self, text, echo=True):
396 self.connection.send(text + '\r')
397 if echo:
398 self.connection.expect(text)
399
400if __name__ == "__main__":
Steve McIntyref1699322014-08-19 22:49:43 +0100401# p = CiscoSX300('10.172.2.52', 23)
402 p = CiscoSX300('10.0.3.15', 23)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100403 p.switch_connect('cisco', 'cisco')
Steve McIntyrecc297112014-08-11 18:46:58 +0100404 #buf = p._show_clock()
405 #print "%s" % buf
406 #buf = p._show_config()
407 #p._dump_list(buf)
408
Steve McIntyre366046f2014-08-18 18:57:03 +0100409 print "System data:"
410 p._dump_list(p._systemdata)
Steve McIntyrecc297112014-08-11 18:46:58 +0100411
412 print "Creating VLANs for testing:"
413 for i in [ 2, 3, 4, 5, 20 ]:
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100414 p.vlan_create(i)
415 p.vlan_set_name(i, "test%d" % i)
Steve McIntyrecc297112014-08-11 18:46:58 +0100416 print " %d (test%d)" % (i, i)
417
418 #print "And dump config\n"
419 #buf = p._show_config()
420 #print "%s" % buf
421
422 #print "Destroying VLAN 2\n"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100423 #p.vlan_destroy(2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100424
425 #print "And dump config\n"
426 #buf = p._show_config()
427 #print "%s" % buf
428
429 #print "Port names are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100430 #buf = p.switch_get_port_names()
Steve McIntyrecc297112014-08-11 18:46:58 +0100431 #p._dump_list(buf)
432
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100433 #buf = p.vlan_get_name(25)
Steve McIntyrecc297112014-08-11 18:46:58 +0100434 #print "VLAN with tag 25 is called \"%s\"" % buf
435
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100436 #p.vlan_set_name(35, "foo")
Steve McIntyrecc297112014-08-11 18:46:58 +0100437 #print "VLAN with tag 35 is called \"foo\""
438
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100439 #buf = p.port_get_mode("fa12")
Steve McIntyrecc297112014-08-11 18:46:58 +0100440 #print "Port fa12 is in %s mode" % buf
441
Steve McIntyre9936d002014-10-01 15:54:10 +0100442 # Test access stuff
443 print "Set fa6 to access mode"
444 p.port_set_mode("fa6", "access")
Steve McIntyre16f02162014-08-14 17:47:36 +0100445 print "Move fa6 to VLAN 2"
Steve McIntyre9936d002014-10-01 15:54:10 +0100446 p.port_set_access_vlan("fa6", 2)
447 buf = p.port_get_access_vlan("fa6")
Steve McIntyrecc297112014-08-11 18:46:58 +0100448 print "Read from switch: fa6 is on VLAN %s" % buf
Steve McIntyre16f02162014-08-14 17:47:36 +0100449 print "Move fa6 back to default VLAN 1"
Steve McIntyre9936d002014-10-01 15:54:10 +0100450 p.port_set_access_vlan("fa6", 1)
Steve McIntyrecc297112014-08-11 18:46:58 +0100451 #print "And move fa6 back to a trunk port"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100452 #p.port_set_mode("fa6", "trunk")
453 #buf = p.port_get_mode("fa6")
Steve McIntyrecc297112014-08-11 18:46:58 +0100454 #print "Port fa6 is in %s mode" % buf
455
456 # Test trunk stuff
457 print "Set gi2 to trunk mode"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100458 p.port_set_mode("gi2", "trunk")
Steve McIntyrecc297112014-08-11 18:46:58 +0100459 print "Add gi2 to VLAN 2"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100460 p.port_add_trunk_to_vlan("gi2", 2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100461 print "Add gi2 to VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100462 p.port_add_trunk_to_vlan("gi2", 3)
Steve McIntyrecc297112014-08-11 18:46:58 +0100463 print "Add gi2 to VLAN 4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100464 p.port_add_trunk_to_vlan("gi2", 4)
Steve McIntyrecc297112014-08-11 18:46:58 +0100465 print "Read from switch: which VLANs is gi2 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100466 buf = p.port_get_trunk_vlan_list("gi2")
Steve McIntyrecc297112014-08-11 18:46:58 +0100467 p._dump_list(buf)
468
Steve McIntyre366046f2014-08-18 18:57:03 +0100469 print "Remove gi2 from VLANs 3,3,4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100470 p.port_remove_trunk_from_vlan("gi2", 3)
471 p.port_remove_trunk_from_vlan("gi2", 3)
472 p.port_remove_trunk_from_vlan("gi2", 4)
Steve McIntyrecc297112014-08-11 18:46:58 +0100473 print "Read from switch: which VLANs is gi2 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100474 buf = p.port_get_trunk_vlan_list("gi2")
Steve McIntyrecc297112014-08-11 18:46:58 +0100475 p._dump_list(buf)
476
477 # print "Adding lots of ports to VLANs"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100478 # p.port_add_trunk_to_vlan("fa1", 2)
479 # p.port_add_trunk_to_vlan("fa3", 2)
480 # p.port_add_trunk_to_vlan("fa5", 2)
481 # p.port_add_trunk_to_vlan("fa7", 2)
482 # p.port_add_trunk_to_vlan("fa9", 2)
483 # p.port_add_trunk_to_vlan("fa11", 2)
484 # p.port_add_trunk_to_vlan("fa13", 2)
485 # p.port_add_trunk_to_vlan("fa15", 2)
486 # p.port_add_trunk_to_vlan("fa17", 2)
487 # p.port_add_trunk_to_vlan("fa19", 2)
488 # p.port_add_trunk_to_vlan("fa21", 2)
489 # p.port_add_trunk_to_vlan("fa23", 2)
490 # p.port_add_trunk_to_vlan("gi4", 2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100491
492 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100493 buf = p.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100494 p._dump_list(buf)
495
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100496# p.switch_save_running_config()
Steve McIntyrecc297112014-08-11 18:46:58 +0100497
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100498 p.switch_disconnect()
Steve McIntyrecc297112014-08-11 18:46:58 +0100499# p._show_config()
500