blob: 75ae7836e5283f06865c1338134db793ffb55ee4 [file] [log] [blame]
Steve McIntyred6759dd2014-08-12 18:10:00 +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 CiscoCatalyst(SwitchDriver):
28
29 connection = None
Steve McIntyre3f287882014-08-18 19:02:15 +010030
31 _capabilities = [
32 'TrunkWildCardVlans' # Trunk ports are on all VLANs by
33 # default, so we shouldn't need to
34 # bugger with them
35 ]
36
Steve McIntyred6759dd2014-08-12 18:10:00 +010037 # Regexp of expected hardware information - fail if we don't see
38 # this
Steve McIntyre3f287882014-08-18 19:02:15 +010039 _expected_descr_re = re.compile('WS-C\S+-\d+P')
Steve McIntyred6759dd2014-08-12 18:10:00 +010040
41 logfile = sys.stderr
42 logfile = None
43
44 def __init__(self, switch_hostname, switch_telnetport=23):
45 self.exec_string = "/usr/bin/telnet %s %d" % (switch_hostname, switch_telnetport)
46
47 ################################
48 ### Switch-level API functions
49 ################################
50
51 # Connect to the switch and log in
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010052 def switch_connect(self, username, password, enablepassword):
Steve McIntyred6759dd2014-08-12 18:10:00 +010053 logging.debug("Connecting to Switch with: %s" % self.exec_string)
54 self.connection = pexpect.spawn(self.exec_string, logfile = self.logfile)
55 self._login(username, password, enablepassword)
56
57 # Try to avoid paged output
58 self.connection.setwinsize(132,1000)
59
60 # And grab details about the switch. in case we need it
Steve McIntyre3f287882014-08-18 19:02:15 +010061 self._get_systemdata()
Steve McIntyred6759dd2014-08-12 18:10:00 +010062
63 # And also validate them - make sure we're driving a switch of
64 # the correct model! Also store the serial number
65 descr_regex = re.compile('^cisco\s+(\S+)')
66 sn_regex = re.compile('System serial number\s+:\s+(\S+)')
67 descr = ""
68
Steve McIntyre3f287882014-08-18 19:02:15 +010069 for line in self._systemdata:
Steve McIntyred6759dd2014-08-12 18:10:00 +010070 match = descr_regex.match(line)
71 if match:
72 descr = match.group(1)
73 match = sn_regex.match(line)
74 if match:
75 self.serial_number = match.group(1)
76
77 print "serial number is %s" % self.serial_number
78 print "system description is %s" % descr
79
Steve McIntyre3f287882014-08-18 19:02:15 +010080 if not self._expected_descr_re.match(descr):
Steve McIntyred6759dd2014-08-12 18:10:00 +010081 raise IOError("Switch %s not recognised by this driver: abort" % descr)
82
83 # Now build a list of our ports, for later sanity checking
Steve McIntyre3f287882014-08-18 19:02:15 +010084 self._ports = self._get_port_names()
85 if len(self._ports) < 4:
Steve McIntyred6759dd2014-08-12 18:10:00 +010086 raise IOError("Not enough ports detected - problem!")
87
88 # Log out of the switch and drop the connection and all state
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010089 def switch_disconnect(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +010090 self._logout()
91 logging.debug("Closing connection: %s" % self.connection)
92 self.connection.close(True)
93 del(self)
94
95 # Save the current running config into flash - we want config to
96 # remain across reboots
Steve McIntyre9b09b9d2014-09-24 15:08:10 +010097 def switch_save_running_config(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +010098 self._cli("copy running-config startup-config")
Steve McIntyree1bf11a2014-08-14 17:56:25 +010099 self.connection.expect("startup-config")
100 self._cli("startup-config")
101 self.connection.expect("OK")
Steve McIntyred6759dd2014-08-12 18:10:00 +0100102
Steve McIntyre3f287882014-08-18 19:02:15 +0100103 # List the capabilities of the switch (and driver) - some things
104 # make no sense to abstract. Returns a dict of strings, each one
105 # describing an extra feature that that higher levels may care
106 # about
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100107 def switch_get_capabilities(self):
Steve McIntyre3f287882014-08-18 19:02:15 +0100108 return self._capabilities
Steve McIntyred6759dd2014-08-12 18:10:00 +0100109
110 ################################
111 ### VLAN API functions
112 ################################
113
114 # Create a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100115 def vlan_create(self, tag):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100116 logging.debug("Creating VLAN %d" % tag)
117 self._configure()
118 self._cli("vlan %d" % tag)
119 self._end_configure()
120
121 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100122 vlans = self.vlan_get_list()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100123 for vlan in vlans:
124 if vlan == tag:
125 return
126 raise IOError("Failed to create VLAN %d" % tag)
127
128 # Destroy a VLAN with the specified tag
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100129 def vlan_destroy(self, tag):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100130 logging.debug("Destroying VLAN %d" % tag)
131 self._configure()
132 self._cli("no vlan %d" % tag)
133 self._end_configure()
134
135 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100136 vlans = self.vlan_get_list()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100137 for vlan in vlans:
138 if vlan == tag:
139 raise IOError("Failed to destroy VLAN %d" % tag)
140
141 # Set the name of a VLAN
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100142 def vlan_set_name(self, tag, name):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100143 logging.debug("Setting name of VLAN %d to %s" % (tag, name))
144 self._configure()
145 self._cli("vlan %d" % tag)
146 self._cli("name %s" % name)
147 self._end_configure()
148
149 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100150 read_name = self.vlan_get_name(tag)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100151 if read_name != name:
152 raise IOError("Failed to set name for VLAN %d (name found is \"%s\", not \"%s\")"
153 % (tag, read_name, name))
154
155 # Get a list of the VLAN tags currently registered on the switch
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100156 def vlan_get_list(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100157 logging.debug("Grabbing list of VLANs")
158 vlans = []
159
160 regex = re.compile('^ *(\d+).*(active)')
161
162 self._cli("show vlan brief")
163 for line in self._read_paged_output():
164 match = regex.match(line)
165 if match:
166 vlans.append(int(match.group(1)))
167 return vlans
168
169 # For a given VLAN tag, ask the switch what the associated name is
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100170 def vlan_get_name(self, tag):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100171 logging.debug("Grabbing the name of VLAN %d" % tag)
172 name = None
173 regex = re.compile('^ *\d+\s+(\S+).*(active)')
174 self._cli("show vlan id %d" % tag)
175 for line in self._read_paged_output():
176 match = regex.match(line)
177 if match:
178 name = match.group(1)
179 name.strip()
180 return name
181
182
183 ################################
184 ### Port API functions
Steve McIntyree1bf11a2014-08-14 17:56:25 +0100185 ################################
Steve McIntyred6759dd2014-08-12 18:10:00 +0100186
Steve McIntyre9936d002014-10-01 15:54:10 +0100187 # Set the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100188 def port_set_mode(self, port, mode):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100189 logging.debug("Setting port %s to %s" % (port, mode))
190 if not self._is_port_mode_valid(mode):
191 raise IndexError("Port mode %s is not allowed" % mode)
192 if not self._is_port_name_valid(port):
193 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100194
Steve McIntyred6759dd2014-08-12 18:10:00 +0100195 self._configure()
196 self._cli("interface %s" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100197 self._cli("switchport mode %s" % mode)
198 if mode == "trunk":
199 self._cli("switchport trunk encapsulation dot1q")
Steve McIntyred6759dd2014-08-12 18:10:00 +0100200 self._end_configure()
201
202 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100203 read_mode = self.port_get_mode(port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100204
Steve McIntyred6759dd2014-08-12 18:10:00 +0100205 if read_mode != mode:
206 raise IOError("Failed to set mode for port %s" % port)
207
Steve McIntyre9936d002014-10-01 15:54:10 +0100208 # Get the mode of a port: access or trunk
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100209 def port_get_mode(self, port):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100210 logging.debug("Getting mode of port %s" % port)
211 mode = ''
212 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 regex = re.compile('Administrative Mode: (.*)')
Steve McIntyreb7adc782014-08-13 00:22:21 +0100215 self._cli("show interfaces %s switchport" % port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100216 for line in self._read_paged_output():
217 match = regex.match(line)
218 if match:
219 mode = match.group(1)
Steve McIntyre9936d002014-10-01 15:54:10 +0100220 if mode == 'static access':
221 return 'access'
Steve McIntyre3f287882014-08-18 19:02:15 +0100222 return mode
Steve McIntyred6759dd2014-08-12 18:10:00 +0100223
Steve McIntyre9936d002014-10-01 15:54:10 +0100224 # Set an access port to be in a specified VLAN (tag)
225 def port_set_access_vlan(self, port, tag):
226 logging.debug("Setting access port %s to VLAN %d" % (port, tag))
Steve McIntyred6759dd2014-08-12 18:10:00 +0100227 if not self._is_port_name_valid(port):
228 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100229 if not (self.port_get_mode(port) == "access"):
230 raise IndexError("Port %s not in access mode" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100231
Steve McIntyred6759dd2014-08-12 18:10:00 +0100232 self._configure()
233 self._cli("interface %s" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100234 self._cli("switchport access vlan %d" % tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100235 self._cli("no shutdown")
Steve McIntyred6759dd2014-08-12 18:10:00 +0100236 self._end_configure()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100237
Steve McIntyre3f287882014-08-18 19:02:15 +0100238 # Finally, validate things worked
Steve McIntyre9936d002014-10-01 15:54:10 +0100239 read_vlan = int(self.port_get_access_vlan(port))
Steve McIntyred6759dd2014-08-12 18:10:00 +0100240 if read_vlan != tag:
Steve McIntyre9936d002014-10-01 15:54:10 +0100241 raise IOError("Failed to move access port %d to VLAN %d - got VLAN %d instead"
Steve McIntyred6759dd2014-08-12 18:10:00 +0100242 % (port, tag, read_vlan))
243
Steve McIntyred6759dd2014-08-12 18:10:00 +0100244 # Add a trunk port to a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100245 def port_add_trunk_to_vlan(self, port, tag):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100246 logging.debug("Adding trunk port %s to VLAN %d" % (port, tag))
247 if not self._is_port_name_valid(port):
248 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100249 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100250 raise IndexError("Port %s not in trunk mode" % port)
251 self._configure()
252 self._cli("interface %s" % port)
253 self._cli("switchport trunk allowed vlan add %d" % tag)
254 self._end_configure()
255
256 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100257 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100258 for vlan in read_vlans:
Steve McIntyre3f287882014-08-18 19:02:15 +0100259 if vlan == tag or vlan == "ALL":
Steve McIntyred6759dd2014-08-12 18:10:00 +0100260 return
Steve McIntyre3f287882014-08-18 19:02:15 +0100261 raise IOError("Failed to add trunk port %s to VLAN %d" % (port, tag))
Steve McIntyred6759dd2014-08-12 18:10:00 +0100262
263 # Remove a trunk port from a specified VLAN (tag)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100264 def port_remove_trunk_from_vlan(self, port, tag):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100265 logging.debug("Removing trunk port %s from VLAN %d" % (port, tag))
266 if not self._is_port_name_valid(port):
267 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100268 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100269 raise IndexError("Port %s not in trunk mode" % port)
270 self._configure()
271 self._cli("interface %s" % port)
272 self._cli("switchport trunk allowed vlan remove %d" % tag)
273 self._end_configure()
274
275 # Validate it happened
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100276 read_vlans = self.port_get_trunk_vlan_list(port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100277 for vlan in read_vlans:
278 if vlan == tag:
Steve McIntyre3f287882014-08-18 19:02:15 +0100279 raise IOError("Failed to remove trunk port %s from VLAN %d" % (port, tag))
Steve McIntyred6759dd2014-08-12 18:10:00 +0100280
Steve McIntyre9936d002014-10-01 15:54:10 +0100281 # Get the configured VLAN tag for an access port (tag)
282 def port_get_access_vlan(self, port):
283 logging.debug("Getting VLAN for access port %s" % port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100284 vlan = 1
285 if not self._is_port_name_valid(port):
286 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9936d002014-10-01 15:54:10 +0100287 if not (self.port_get_mode(port) == "access"):
288 raise IndexError("Port %s not in access mode" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100289 regex = re.compile('Access Mode VLAN: (\d+)')
290 self._cli("show interfaces %s switchport" % port)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100291 for line in self._read_paged_output():
292 match = regex.match(line)
293 if match:
294 vlan = match.group(1)
295 return int(vlan)
296
297 # Get the list of configured VLAN tags for a trunk port
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100298 def port_get_trunk_vlan_list(self, port):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100299 logging.debug("Getting VLANs for trunk port %s" % port)
300 vlans = [ ]
301 if not self._is_port_name_valid(port):
302 raise IndexError("Port name %s not recognised" % port)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100303 if not (self.port_get_mode(port) == "trunk"):
Steve McIntyre9936d002014-10-01 15:54:10 +0100304 raise IndexError("Port %s not in access mode" % port)
Steve McIntyre3f287882014-08-18 19:02:15 +0100305 regex_start = re.compile('Trunking VLANs Enabled: (.*)')
306 regex_continue = re.compile('\s*(\d.*)')
307 self._cli("show interfaces %s switchport" % port)
308
309 # Horrible parsing work - VLAN list may extend over several lines
310 in_match = False
311 vlan_text = ''
312
Steve McIntyred6759dd2014-08-12 18:10:00 +0100313 for line in self._read_paged_output():
Steve McIntyre3f287882014-08-18 19:02:15 +0100314 if in_match:
315 match = regex_continue.match(line)
316 if match:
317 vlan_text += match.group(1)
318 next
319 else:
320 in_match = False
321 next
322 else:
323 match = regex_start.match(line)
324 if match:
325 vlan_text += match.group(1)
326 in_match = True
327
328 vlans = self._parse_vlan_list(vlan_text)
329
Steve McIntyred6759dd2014-08-12 18:10:00 +0100330 return vlans
331
332 ################################
333 ### Internal functions
334 ################################
335
336 def _login(self, username, password, enablepassword):
337 logging.debug("attempting login with username %s, password %s" % (username, password))
338 self.connection.expect('User Access Verification')
339 if username is not None:
340 self.connection.expect("User Name:")
341 self._cli("%s" % username)
342 if password is not None:
343 self.connection.expect("Password:")
344 self._cli("%s" % password, False)
345 while True:
346 index = self.connection.expect(['User Name:', 'Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)'])
347 if index != 4: # Any other means: failed to log in!
348 logging.error("Login failure: index %d\n" % index)
349 logging.error("Login failure: %s\n" % self.connection.match.before)
350 raise IOError
351
352 # else
Steve McIntyre3f287882014-08-18 19:02:15 +0100353 self._prompt_name = self.connection.match.group(1).strip()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100354 if self.connection.match.group(2) == ">":
355 # Need to enter "enable" mode too
356 self._cli("enable")
357 if enablepassword is not None:
358 self.connection.expect("Password:")
359 self._cli("%s" % enablepassword, False)
360 index = self.connection.expect(['Password:', 'Bad passwords', 'authentication failed', r'(.*)(#|>)'])
361 if index != 3: # Any other means: failed to log in!
362 logging.error("Enable password failure: %s\n" % self.connection.match)
363 raise IOError
364 return 0
365
366 def _logout(self):
367 logging.debug("Logging out")
368 self._cli("exit", False)
369
370 def _configure(self):
371 self._cli("configure terminal")
372
373 def _end_configure(self):
374 self._cli("end")
375
376 def _read_paged_output(self):
377 buf = []
Steve McIntyre3f287882014-08-18 19:02:15 +0100378 prompt = self._prompt_name + '#'
Steve McIntyred6759dd2014-08-12 18:10:00 +0100379 while True:
380 index = self.connection.expect([' -*More-*', prompt])
381 if index == 0: # More: <space>
382 for line in self.connection.before.split('\r\n'):
383 line1 = re.sub('(\x08|\x0D)*', '', line.strip())
384 buf.append(line1)
385 self._cli(' ', False)
386 elif index == 1: # Back to a prompt, says output is finished
387 break
388
389 for line in self.connection.before.split('\r\n'):
390 line1 = re.sub('(\x08|\x0D)*', '', line.strip())
391 buf.append(line1)
392
393 return buf
394
395 def _get_port_names(self):
396 logging.debug("Grabbing list of ports")
397 interfaces = []
398
399 # Use "Up" or "Down" to only identify lines in the output that
400 # match interfaces that exist
401 regex = re.compile('^\s*([a-zA-Z0-9_/]*).*(connect)(.*)')
402 regex1 = re.compile('.*Not Present.*')
403
404 self._cli("show interfaces status")
405 for line in self._read_paged_output():
406 match = regex.match(line)
407 if match:
408 interface = match.group(1)
409 junk = match.group(3)
410 match1 = regex1.match(junk) # Deliberately drop things
411 # marked as "Not Present"
412 if not match1:
413 interfaces.append(interface)
414 return interfaces
415
Steve McIntyred6759dd2014-08-12 18:10:00 +0100416 def _show_config(self):
417 logging.debug("Grabbing config")
418 self._cli("show running-config")
419 return self._read_paged_output()
420
421 def _show_clock(self):
422 logging.debug("Grabbing time")
423 self._cli("show clock")
424 return self._read_paged_output()
425
Steve McIntyred6759dd2014-08-12 18:10:00 +0100426 def _get_systemdata(self):
Steve McIntyred6759dd2014-08-12 18:10:00 +0100427 logging.debug("Grabbing system sw and hw versions")
Steve McIntyreffb9b5a2014-10-10 16:31:58 +0100428
Steve McIntyred6759dd2014-08-12 18:10:00 +0100429 self._cli("show version")
Steve McIntyreffb9b5a2014-10-10 16:31:58 +0100430 self._systemdata = []
Steve McIntyred6759dd2014-08-12 18:10:00 +0100431 for line in self._read_paged_output():
Steve McIntyre3f287882014-08-18 19:02:15 +0100432 self._systemdata.append(line)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100433
Steve McIntyre3f287882014-08-18 19:02:15 +0100434 def _parse_vlan_list(self, input):
435 vlans = []
436
437 if input == "ALL":
438 return ["ALL"]
439 elif input == "NONE":
440 return []
441 else:
442 # Parse the complex list
443 groups = input.split(',')
444 for group in groups:
445 subgroups = group.split('-')
446 if len(subgroups) == 1:
447 vlans.append(int(subgroups[0]))
448 elif len(subgroups) == 2:
449 for i in range (int(subgroups[0]), int(subgroups[1]) + 1):
450 vlans.append(i)
451 else:
452 print "Can't parse group \"" + group + "\""
453
454 return vlans
Steve McIntyred6759dd2014-08-12 18:10:00 +0100455
456 # Wrapper around connection.send - by default, expect() the same
457 # text we've sent, to remove it from the output from the
458 # switch. For the few cases where we don't need that, override
459 # this using echo=False.
460 # Horrible, but seems to work.
461 def _cli(self, text, echo=True):
462 self.connection.send(text + '\r')
463 if echo:
464 self.connection.expect(text)
465
466if __name__ == "__main__":
467 p = CiscoCatalyst('lngswitch02', 23)
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100468 p.switch_connect(None, 'lngvirtual', 'lngenable')
Steve McIntyred6759dd2014-08-12 18:10:00 +0100469
470 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100471 buf = p.vlan_get_list()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100472 p._dump_list(buf)
473
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100474 buf = p.vlan_get_name(2)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100475 print "VLAN 2 is named \"%s\"" % buf
476
477 print "Create VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100478 p.vlan_create(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100479
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100480 buf = p.vlan_get_name(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100481 print "VLAN 3 is named \"%s\"" % buf
482
483 print "Set name of VLAN 3 to test333"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100484 p.vlan_set_name(3, "test333")
Steve McIntyred6759dd2014-08-12 18:10:00 +0100485
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100486 buf = p.vlan_get_name(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100487 print "VLAN 3 is named \"%s\"" % buf
488
489 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100490 buf = p.vlan_get_list()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100491 p._dump_list(buf)
492
493 print "Destroy VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100494 p.vlan_destroy(3)
Steve McIntyred6759dd2014-08-12 18:10:00 +0100495
496 print "VLANs are:"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100497 buf = p.vlan_get_list()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100498 p._dump_list(buf)
499
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100500 buf = p.port_get_mode("Gi1/0/10")
Steve McIntyreb7adc782014-08-13 00:22:21 +0100501 print "Port Gi1/0/10 is in %s mode" % buf
502
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100503 buf = p.port_get_mode("Gi1/0/11")
Steve McIntyreb7adc782014-08-13 00:22:21 +0100504 print "Port Gi1/0/11 is in %s mode" % buf
Steve McIntyred6759dd2014-08-12 18:10:00 +0100505
Steve McIntyre9936d002014-10-01 15:54:10 +0100506 # Test access stuff
507 print "Set Gi1/0/9 to access mode"
508 p.port_set_mode("Gi1/0/9", "access")
Steve McIntyre3f287882014-08-18 19:02:15 +0100509
510 print "Move Gi1/0/9 to VLAN 4"
Steve McIntyre9936d002014-10-01 15:54:10 +0100511 p.port_set_access_vlan("Gi1/0/9", 4)
Steve McIntyre3f287882014-08-18 19:02:15 +0100512
Steve McIntyre9936d002014-10-01 15:54:10 +0100513 buf = p.port_get_access_vlan("Gi1/0/9")
Steve McIntyre3f287882014-08-18 19:02:15 +0100514 print "Read from switch: Gi1/0/9 is on VLAN %s" % buf
515
516 print "Move Gi1/0/9 back to VLAN 1"
Steve McIntyre9936d002014-10-01 15:54:10 +0100517 p.port_set_access_vlan("Gi1/0/9", 1)
Steve McIntyre3f287882014-08-18 19:02:15 +0100518
Steve McIntyre9936d002014-10-01 15:54:10 +0100519 # Test access stuff
Steve McIntyre3f287882014-08-18 19:02:15 +0100520 print "Set Gi1/0/9 to trunk mode"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100521 p.port_set_mode("Gi1/0/9", "trunk")
Steve McIntyre3f287882014-08-18 19:02:15 +0100522 print "Read from switch: which VLANs is Gi1/0/9 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100523 buf = p.port_get_trunk_vlan_list("Gi1/0/9")
Steve McIntyre3f287882014-08-18 19:02:15 +0100524 p._dump_list(buf)
525 print "Add Gi1/0/9 to VLAN 2"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100526 p.port_add_trunk_to_vlan("Gi1/0/9", 2)
Steve McIntyre3f287882014-08-18 19:02:15 +0100527 print "Add Gi1/0/9 to VLAN 3"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100528 p.port_add_trunk_to_vlan("Gi1/0/9", 3)
Steve McIntyre3f287882014-08-18 19:02:15 +0100529 print "Add Gi1/0/9 to VLAN 4"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100530 p.port_add_trunk_to_vlan("Gi1/0/9", 4)
Steve McIntyre3f287882014-08-18 19:02:15 +0100531 print "Read from switch: which VLANs is Gi1/0/9 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100532 buf = p.port_get_trunk_vlan_list("Gi1/0/9")
Steve McIntyre3f287882014-08-18 19:02:15 +0100533 p._dump_list(buf)
534
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100535 p.port_remove_trunk_from_vlan("Gi1/0/9", 3)
536 p.port_remove_trunk_from_vlan("Gi1/0/9", 3)
537 p.port_remove_trunk_from_vlan("Gi1/0/9", 4)
Steve McIntyre3f287882014-08-18 19:02:15 +0100538 print "Read from switch: which VLANs is Gi1/0/9 on?"
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100539 buf = p.port_get_trunk_vlan_list("Gi1/0/9")
Steve McIntyre3f287882014-08-18 19:02:15 +0100540 p._dump_list(buf)
541
542
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100543# p.switch_save_running_config()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100544
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100545# p.switch_disconnect()
Steve McIntyred6759dd2014-08-12 18:10:00 +0100546# p._show_config()