Python Scripting Examples

Example 1

This is a single line script that illustrates the exsh.clicmd () called from a Python script.

print exsh.clicmd('show switch', True)

In this example, the second parameter passed to exsh.clicmd () is True. This returns the CLI display output that would have normally been sent to the user. In this case, the line prints the output anyway, which is shown below.

SysName:          X670V-48x 
SysContact:, +1 888 257 3000 
System MAC:       00:04:96:6D:12:A7 
System Type:      X670V-48x 

SysHealth check:  Enabled (Normal) 
Recovery Mode:    All 
System Watchdog:  Enabled 

Current Time:     Tue Dec 17 01:49:10 2013 
Timezone:         [Auto DST Disabled] GMT Offset: 0 minutes, name is UTC. 
Boot Time:        Mon Dec 16 00:28:13 2013 
Boot Count:       1559 
Next Reboot:      None scheduled 
System UpTime:    1 day 1 hour 20 minutes 57 seconds 

Current State:    OPERATIONAL 
Image Selected:   secondary 
Image Booted:     secondary 
Primary ver: 
Secondary ver: 
Config Selected:  primary.cfg 
Config Booted:    primary.cfg 
primary.cfg       Created by ExtremeXOS version                   
                  255914 bytes saved on Sun Dec  8 00:04:20 2013 

Example 2

In this example, we create a number of VLANs in the form of prefix tag and add tagged port lists to each VLAN. There are a number of methods for collecting user input and validating the data type. This example uses a simple while True: loop until the input is the correct type. A more robust example would also apply range checking etc… to the vlan tag(s). E.g. Create vlans with tags from 1000 to 1099 The prefix is ‘dave‘, so each vlan name will have a name like ‘dave1000‘, ‘dave1001‘ etc… The user provided port list will be added to each vlan name as tagged ports.

import sys 
while True:     
    prefix = raw_input('enter vlan name prefix:')     
    if len(prefix):         

while True:     
    reply = raw_input('beginning vlan tag:')     
    if reply.isdigit():         
        minTag = int(reply)         

while True:     
   reply = raw_input('ending vlan tag:')     
   if reply.isdigit():         
       maxTag = int(reply)         

if minTag > maxTag:     
    print 'Beginning vlan id cannot be greater than ending vlan id'     

while True:     
    portlist = raw_input('port list:')     
    if len(portlist):         

print 'Creating vlans from ',prefix+str(minTag),'to',prefix+str(maxTag) 

for vlanId in range(minTag,maxTag + 1):     
    vlanName = prefix + '{0:>04d}'.format(vlanId)     
    cmd = 'create vlan '+vlanName + ' tag ' + str(vlanId)     
    print exsh.clicmd(cmd, True)     
    cmd = 'config vlan ' + vlanName + ' add port ' + portlist + ' tag'     
    print exsh.clicmd(cmd, True) 


UPM variables can be used with Python scripting. They must be passed as arguments of the script.