blob: 7cdeb4084f563081f36e7f54ed6d1cd46f1309f6 [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)
Steve McIntyre11393992014-10-10 15:53:34 +010088 self._ports = []
89 self._prompt_name = ''
90 self._systemdata = []
Steve McIntyrecc297112014-08-11 18:46:58 +010091 del(self)
92
93 # Save the current running config into flash - we want config to
94 # remain across reboots
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010095 def switch_save_running_config(self):
Steve McIntyrecc297112014-08-11 18:46:58 +010096 self._cli("copy running-config startup-config")
97 self.connection.expect("Y/N")
98 self._cli("y")
99 self.connection.expect("Copy succeeded")
100
Steve McIntyrecc297112014-08-11 18:46:58 +0100101 ################################
102 ### VLAN API functions
103 ################################
104
105 # Create a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100106 def vlan_create(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100107 logging.debug("Creating VLAN %d" % tag)
108 self._configure()
109 self._cli("vlan database")
110 self._cli("vlan %d" % tag)
111 self._end_configure()
112
113 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100114 vlans = self.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100115 for vlan in vlans:
116 if vlan == tag:
117 return
118 raise IOError("Failed to create VLAN %d" % tag)
119
120 # Destroy a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100121 def vlan_destroy(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100122 logging.debug("Destroying VLAN %d" % tag)
123 self._configure()
124 self._cli("no vlan %d" % tag)
125 self._end_configure()
126
127 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100128 vlans = self.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100129 for vlan in vlans:
130 if vlan == tag:
131 raise IOError("Failed to destroy VLAN %d" % tag)
132
133 # Set the name of a VLAN
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100134 def vlan_set_name(self, tag, name):
Steve McIntyrecc297112014-08-11 18:46:58 +0100135 logging.debug("Setting name of VLAN %d to %s" % (tag, name))
136 self._configure()
137 self._cli("vlan %d" % tag)
138 self._cli("interface vlan %d" % tag)
139 self._cli("name %s" % name)
140 self._end_configure()
141
142 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100143 read_name = self.vlan_get_name(tag)
Steve McIntyrecc297112014-08-11 18:46:58 +0100144 if read_name != name:
145 raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")"
146 % (tag, read_name, name))
147
148 # Get a list of the VLAN tags currently registered on the switch
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100149 def vlan_get_list(self):
Steve McIntyrecc297112014-08-11 18:46:58 +0100150 logging.debug("Grabbing list of VLANs")
151 vlans = []
152
153 regex = re.compile('^ *(\d+).*(D|S|G|R)')
154
155 self._cli("show vlan")
156 for line in self._read_paged_output():
157 match = regex.match(line)
158 if match:
159 vlans.append(int(match.group(1)))
160 return vlans
161
162 # For a given VLAN tag, ask the switch what the associated name is
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100163 def vlan_get_name(self, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100164 logging.debug("Grabbing the name of VLAN %d" % tag)
165 name = None
166 regex = re.compile('^ *\d+\s+(\S+).*(D|S|G|R)')
167 self._cli("show vlan tag %d" % tag)
168 for line in self._read_paged_output():
169 match = regex.match(line)
170 if match:
171 name = match.group(1)
172 name.strip()
173 return name
174
175
176 ################################
177 ### Port API functions
178 ################################
179
Steve McIntyre9936d002014-10-01 15:54:10 +0100180 # Set the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100181 def port_set_mode(self, port, mode):
Steve McIntyrecc297112014-08-11 18:46:58 +0100182 logging.debug("Setting port %s to %s" % (port, mode))
183 if not self._is_port_mode_valid(mode):
184 raise IndexError("Port mode %s is not allowed" % mode)
185 if not self._is_port_name_valid(port):
186 raise IndexError("Port name %s not recognised" % port)
187 self._configure()
188 self._cli("interface %s" % port)
189 self._cli("switchport mode %s" % mode)
190 self._end_configure()
191
192 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100193 read_mode = self.port_get_mode(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100194 if read_mode != mode:
195 raise IOError("Failed to set mode for port %s" % port)
196
197
Steve McIntyre9936d002014-10-01 15:54:10 +0100198 # Get the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100199 def port_get_mode(self, port):
Steve McIntyrecc297112014-08-11 18:46:58 +0100200 logging.debug("Getting mode of port %s" % port)
201 mode = ''
202 if not self._is_port_name_valid(port):
203 raise IndexError("Port name %s not recognised" % port)
204 regex = re.compile('Port Mode: (\S+)')
205 self._cli("show interfaces switchport %s" % port)
206 for line in self._read_paged_output():
207 match = regex.match(line)
208 if match:
209 mode = match.group(1)
210 return mode.lower()
211
Steve McIntyre9936d002014-10-01 15:54:10 +0100212 # Set an access port to be in a specified VLAN (tag)
213 def port_set_access_vlan(self, port, tag):
214 logging.debug("Setting access port %s to VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100215 if not self._is_port_name_valid(port):
216 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100217 if not (self.port_get_mode(port) == "access"):
218 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100219
Steve McIntyre9936d002014-10-01 15:54:10 +0100220 self._configure()
221 self._cli("interface %s" % port)
222 self._cli("switchport access vlan %d" % tag)
223 self._end_configure()
Steve McIntyrecc297112014-08-11 18:46:58 +0100224
Steve McIntyre9936d002014-10-01 15:54:10 +0100225 # Validate things worked
226 read_vlan = int(self.port_get_access_vlan(port))
Steve McIntyrecc297112014-08-11 18:46:58 +0100227 if read_vlan != tag:
Steve McIntyre9936d002014-10-01 15:54:10 +0100228 raise IOError("Failed to move access port %s to VLAN %d - got VLAN %d instead"
Steve McIntyrecc297112014-08-11 18:46:58 +0100229 % (port, tag, read_vlan))
230
Steve McIntyrecc297112014-08-11 18:46:58 +0100231 # Add a trunk port to a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100232 def port_add_trunk_to_vlan(self, port, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100233 logging.debug("Adding trunk port %s to VLAN %d" % (port, tag))
234 if not self._is_port_name_valid(port):
235 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100236 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyrecc297112014-08-11 18:46:58 +0100237 raise IndexError("Port %s not in trunk mode" % port)
238 self._configure()
239 self._cli("interface %s" % port)
240 self._cli("switchport trunk allowed vlan add %d" % tag)
241 self._end_configure()
242
243 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100244 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100245 for vlan in read_vlans:
246 if vlan == tag:
247 return
Steve McIntyre5b3ce212014-08-15 13:10:41 +0100248 raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100249
250 # Remove a trunk port from a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100251 def port_remove_trunk_from_vlan(self, port, tag):
Steve McIntyrecc297112014-08-11 18:46:58 +0100252 logging.debug("Removing trunk port %s from VLAN %d" % (port, tag))
253 if not self._is_port_name_valid(port):
254 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100255 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyrecc297112014-08-11 18:46:58 +0100256 raise IndexError("Port %s not in trunk mode" % port)
257 self._configure()
258 self._cli("interface %s" % port)
259 self._cli("switchport trunk allowed vlan remove %d" % tag)
260 self._end_configure()
261
262 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100263 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100264 for vlan in read_vlans:
265 if vlan == tag:
Steve McIntyre5b3ce212014-08-15 13:10:41 +0100266 raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag))
Steve McIntyrecc297112014-08-11 18:46:58 +0100267
Steve McIntyre9936d002014-10-01 15:54:10 +0100268 # Get the configured VLAN tag for an access port (tag)
269 def port_get_access_vlan(self, port):
270 logging.debug("Getting VLAN for access port %s" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100271 vlan = 1
272 if not self._is_port_name_valid(port):
273 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100274 if not (self.port_get_mode(port) == "access"):
275 raise IndexError("Port %s not in access mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100276 regex = re.compile('(\d+)\s+\S+\s+Untagged\s+Static')
277 self._cli("show interfaces switchport %s" % port)
278 for line in self._read_paged_output():
279 match = regex.match(line)
280 if match:
281 vlan = match.group(1)
282 return int(vlan)
283
284 # Get the list of configured VLAN tags for a trunk port
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100285 def port_get_trunk_vlan_list(self, port):
Steve McIntyrecc297112014-08-11 18:46:58 +0100286 logging.debug("Getting VLANs for trunk port %s" % port)
287 vlans = [ ]
288 if not self._is_port_name_valid(port):
289 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100290 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyree5ea20a2014-10-20 10:39:31 +0100291 raise IndexError("Port %s not in trunk mode" % port)
Steve McIntyrecc297112014-08-11 18:46:58 +0100292 regex = re.compile('(\d+)\s+\S+\s+(Tagged|Untagged)\s+Static')
293 self._cli("show interfaces switchport %s" % port)
294 for line in self._read_paged_output():
295 match = regex.match(line)
296 if match:
297 vlans.append (int(match.group(1)))
298 return vlans
299
300 ################################
301 ### Internal functions
302 ################################
303
304 def _login(self, username, password):
305 logging.debug("attempting login with username %s, password %s" % (username, password))
306 self._cli("")
307 self.connection.expect("User Name:")
308 self._cli("%s" % username)
309 self.connection.expect("Password:")
310 self._cli("%s" % password, False)
311 while True:
312 index = self.connection.expect(['User Name:', 'authentication failed', r'(.*)#', '.*'])
313 if index == 0 or index == 1: # Failed to log in!
314 logging.error("Login failure: %s\n" % self.connection.match)
315 raise IOError
316 elif index == 2:
Steve McIntyre366046f2014-08-18 18:57:03 +0100317 self._prompt_name = self.connection.match.group(1).strip()
Steve McIntyrecc297112014-08-11 18:46:58 +0100318 return 0
319
320 def _logout(self):
321 logging.debug("Logging out")
322 self._cli("exit", False)
323
Steve McIntyrecc297112014-08-11 18:46:58 +0100324 def _configure(self):
325 self._cli("configure terminal")
326
327 def _end_configure(self):
328 self._cli("end")
329
330 def _read_paged_output(self):
331 buf = []
Steve McIntyre366046f2014-08-18 18:57:03 +0100332 prompt = self._prompt_name + '#'
Steve McIntyrecc297112014-08-11 18:46:58 +0100333 while True:
334 index = self.connection.expect(['\x1b\[0mMore:.*<return>.*$', prompt])
335 if index == 0: # More: <space>
336 for line in self.connection.before.split('\r\n'):
337 buf.append(line.strip())
338 self._cli(' ', False)
339 elif index == 1: # Back to a prompt, says output is finished
340 break
341
342 for line in self.connection.before.split('\r\n'):
343 buf.append(line.strip())
344
345 return buf
346
347 def _get_port_names(self):
348 logging.debug("Grabbing list of ports")
349 interfaces = []
350
351 # Use "Up" or "Down" to only identify lines in the output that
352 # match interfaces that exist
353 regex = re.compile('^(\w+).*(Up|Down)')
354
355 self._cli("show interfaces status detailed")
356 for line in self._read_paged_output():
357 match = regex.match(line)
358 if match:
359 interfaces.append(match.group(1))
360 return interfaces
361
Steve McIntyrecc297112014-08-11 18:46:58 +0100362 def _show_config(self):
363 logging.debug("Grabbing config")
364 self._cli("show running-config")
365 return self._read_paged_output()
366
367 def _show_clock(self):
368 logging.debug("Grabbing time")
369 self._cli("show clock")
370 return self._read_paged_output()
371
Steve McIntyrecc297112014-08-11 18:46:58 +0100372 def _get_systemdata(self):
Steve McIntyrecc297112014-08-11 18:46:58 +0100373
Steve McIntyreffb9b5a2014-10-10 16:31:58 +0100374 self._systemdata = []
375
Steve McIntyre28d34ca2014-08-12 15:40:24 +0100376 logging.debug("Grabbing system data")
Steve McIntyrecc297112014-08-11 18:46:58 +0100377 self._cli("show system")
378 for line in self._read_paged_output():
Steve McIntyre366046f2014-08-18 18:57:03 +0100379 self._systemdata.append(line)
Steve McIntyre28d34ca2014-08-12 15:40:24 +0100380
381 logging.debug("Grabbing system sw and hw versions")
382 self._cli("show version")
383 for line in self._read_paged_output():
Steve McIntyre366046f2014-08-18 18:57:03 +0100384 self._systemdata.append(line)
Steve McIntyrecc297112014-08-11 18:46:58 +0100385
Steve McIntyre16f02162014-08-14 17:47:36 +0100386 ######################################
387 # Internal port access helper methods
388 ######################################
389 # N.B. No parameter checking here, for speed reasons - if you're
390 # calling this internal API then you should already have validated
391 # things yourself! Equally, no post-set checks in here - do that
392 # at the higher level.
393 ######################################
394
Steve McIntyrecc297112014-08-11 18:46:58 +0100395 # Wrapper around connection.send - by default, expect() the same
396 # text we've sent, to remove it from the output from the
397 # switch. For the few cases where we don't need that, override
398 # this using echo=False.
399 # Horrible, but seems to work.
400 def _cli(self, text, echo=True):
401 self.connection.send(text + '\r')
402 if echo:
403 self.connection.expect(text)
404
405if __name__ == "__main__":
Steve McIntyref1699322014-08-19 22:49:43 +0100406# p = CiscoSX300('10.172.2.52', 23)
407 p = CiscoSX300('10.0.3.15', 23)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100408 p.switch_connect('cisco', 'cisco')
Steve McIntyrecc297112014-08-11 18:46:58 +0100409 #buf = p._show_clock()
410 #print "%s" % buf
411 #buf = p._show_config()
412 #p._dump_list(buf)
413
Steve McIntyre366046f2014-08-18 18:57:03 +0100414 print "System data:"
415 p._dump_list(p._systemdata)
Steve McIntyrecc297112014-08-11 18:46:58 +0100416
417 print "Creating VLANs for testing:"
418 for i in [ 2, 3, 4, 5, 20 ]:
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100419 p.vlan_create(i)
420 p.vlan_set_name(i, "test%d" % i)
Steve McIntyrecc297112014-08-11 18:46:58 +0100421 print " %d (test%d)" % (i, i)
422
423 #print "And dump config\n"
424 #buf = p._show_config()
425 #print "%s" % buf
426
427 #print "Destroying VLAN 2\n"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100428 #p.vlan_destroy(2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100429
430 #print "And dump config\n"
431 #buf = p._show_config()
432 #print "%s" % buf
433
434 #print "Port names are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100435 #buf = p.switch_get_port_names()
Steve McIntyrecc297112014-08-11 18:46:58 +0100436 #p._dump_list(buf)
437
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100438 #buf = p.vlan_get_name(25)
Steve McIntyrecc297112014-08-11 18:46:58 +0100439 #print "VLAN with tag 25 is called \"%s\"" % buf
440
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100441 #p.vlan_set_name(35, "foo")
Steve McIntyrecc297112014-08-11 18:46:58 +0100442 #print "VLAN with tag 35 is called \"foo\""
443
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100444 #buf = p.port_get_mode("fa12")
Steve McIntyrecc297112014-08-11 18:46:58 +0100445 #print "Port fa12 is in %s mode" % buf
446
Steve McIntyre9936d002014-10-01 15:54:10 +0100447 # Test access stuff
448 print "Set fa6 to access mode"
449 p.port_set_mode("fa6", "access")
Steve McIntyre16f02162014-08-14 17:47:36 +0100450 print "Move fa6 to VLAN 2"
Steve McIntyre9936d002014-10-01 15:54:10 +0100451 p.port_set_access_vlan("fa6", 2)
452 buf = p.port_get_access_vlan("fa6")
Steve McIntyrecc297112014-08-11 18:46:58 +0100453 print "Read from switch: fa6 is on VLAN %s" % buf
Steve McIntyre16f02162014-08-14 17:47:36 +0100454 print "Move fa6 back to default VLAN 1"
Steve McIntyre9936d002014-10-01 15:54:10 +0100455 p.port_set_access_vlan("fa6", 1)
Steve McIntyrecc297112014-08-11 18:46:58 +0100456 #print "And move fa6 back to a trunk port"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100457 #p.port_set_mode("fa6", "trunk")
458 #buf = p.port_get_mode("fa6")
Steve McIntyrecc297112014-08-11 18:46:58 +0100459 #print "Port fa6 is in %s mode" % buf
460
461 # Test trunk stuff
462 print "Set gi2 to trunk mode"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100463 p.port_set_mode("gi2", "trunk")
Steve McIntyrecc297112014-08-11 18:46:58 +0100464 print "Add gi2 to VLAN 2"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100465 p.port_add_trunk_to_vlan("gi2", 2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100466 print "Add gi2 to VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100467 p.port_add_trunk_to_vlan("gi2", 3)
Steve McIntyrecc297112014-08-11 18:46:58 +0100468 print "Add gi2 to VLAN 4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100469 p.port_add_trunk_to_vlan("gi2", 4)
Steve McIntyrecc297112014-08-11 18:46:58 +0100470 print "Read from switch: which VLANs is gi2 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100471 buf = p.port_get_trunk_vlan_list("gi2")
Steve McIntyrecc297112014-08-11 18:46:58 +0100472 p._dump_list(buf)
473
Steve McIntyre366046f2014-08-18 18:57:03 +0100474 print "Remove gi2 from VLANs 3,3,4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100475 p.port_remove_trunk_from_vlan("gi2", 3)
476 p.port_remove_trunk_from_vlan("gi2", 3)
477 p.port_remove_trunk_from_vlan("gi2", 4)
Steve McIntyrecc297112014-08-11 18:46:58 +0100478 print "Read from switch: which VLANs is gi2 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100479 buf = p.port_get_trunk_vlan_list("gi2")
Steve McIntyrecc297112014-08-11 18:46:58 +0100480 p._dump_list(buf)
481
482 # print "Adding lots of ports to VLANs"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100483 # p.port_add_trunk_to_vlan("fa1", 2)
484 # p.port_add_trunk_to_vlan("fa3", 2)
485 # p.port_add_trunk_to_vlan("fa5", 2)
486 # p.port_add_trunk_to_vlan("fa7", 2)
487 # p.port_add_trunk_to_vlan("fa9", 2)
488 # p.port_add_trunk_to_vlan("fa11", 2)
489 # p.port_add_trunk_to_vlan("fa13", 2)
490 # p.port_add_trunk_to_vlan("fa15", 2)
491 # p.port_add_trunk_to_vlan("fa17", 2)
492 # p.port_add_trunk_to_vlan("fa19", 2)
493 # p.port_add_trunk_to_vlan("fa21", 2)
494 # p.port_add_trunk_to_vlan("fa23", 2)
495 # p.port_add_trunk_to_vlan("gi4", 2)
Steve McIntyrecc297112014-08-11 18:46:58 +0100496
497 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100498 buf = p.vlan_get_list()
Steve McIntyrecc297112014-08-11 18:46:58 +0100499 p._dump_list(buf)
500
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100501# p.switch_save_running_config()
Steve McIntyrecc297112014-08-11 18:46:58 +0100502
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100503 p.switch_disconnect()
Steve McIntyrecc297112014-08-11 18:46:58 +0100504# p._show_config()
505