Lab4 EMC Python: Take Action

from jsonrpc import JsonRPC

def remote_cli(jsonrpc, cmd):
# ###################################################################
# Use the jsonrpc.cli() method to send CLI commands to an IP address
# cmd = a single CLI command string or a list of CLI command strings
# ###################################################################
response = jsonrpc.cli(cmd)

#print json.dumps(response, indent=2, sort_keys=True) # Uncomment for debugging

if isinstance(response, dict):
result = response.get(‘result’)
for entry in result:
#print “Debug: Entry = “, entry # Uncomment for debugging
if ‘status’ in entry and entry.get(‘status’) == ‘ERROR’:
raise RuntimeError(“Command generated error on switch”)

return result

def main():

familyType = emc_vars[‘family’]
switchIpaddress = emc_vars[‘deviceIP’]
switchUsername = emc_vars[‘deviceLogin’]
switchPassword = emc_vars[‘devicePwd’]

if familyType == ‘VSP Series’:
print “This script uses JSON and can only be used with XOS”
raise RuntimeError(“This script will only work on XOS switches”)

# create a JSONRPC interface object with any defaults
jsonrpcObj = JsonRPC(ipaddress=switchIpaddress, username=switchUsername, password=switchPassword)

cmd = ‘show ports description’
result = remote_cli(jsonrpcObj, cmd)

burnPorts = []
for entry in result:
#print “Debug: Entry = “, entry # Uncomment for debugging
if ‘show_ports_description’ in entry:
port = entry.get(‘show_ports_description’).get(‘port’)
name = entry.get(‘show_ports_description’).get(‘displayString’)
print “Debug: Got port {} with name = {}”.format(port, name)
if name == ‘Burn-now’:
burnPorts.append(port)

if burnPorts:
portList = ‘,’.join(str(x) for x in burnPorts)

cmd = ‘disable ports ‘ + portList
remote_cli(jsonrpcObj, cmd)

cmd = ‘unconfigure ports ‘ + portList + ‘ display-string’
remote_cli(jsonrpcObj, cmd)

cmd = ‘enable ports ‘ + portList
remote_cli(jsonrpcObj, cmd)

print “Successfully burned ports: “, portList

else:
print “No ports to burn!”

main()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s