Charming The Python On Nexus 7000 Switch v1: About This Solution
Charming The Python On Nexus 7000 Switch v1: About This Solution
In this demonstration, you will run through five scenarios that will cover some basic Python programming, and introduce examples
on how the Python API can be used to interact with the Nexus switch:
This demonstration is built using Virtual Internet Routing Lab (VIRL). VIRL is a multi-purpose network virtualization platform that
provides an easy way to build, configure, and test new or existing network topologies with an intuitive user interface.
For additional information on the Nexus Python API, go to N7K Python API.
For additional information on VIRL, watch VIRL (Virtual Internet Routing Lab).
Requirements
Table 1. Requirements
Required Optional
Topology
This demonstration contains preconfigured users and components to illustrate the capabilities available to you within this sandbox.
You can see the IP address and user account credentials to use to access a component by clicking the component icon in the
Topology menu of your active session and in the scenario steps that require their use.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 1 of 19
Cisco dCloud
Figure 1. Topology
The VIRL topology used for this demonstration, depicted as a single server above, actually consists of:
• Two Nexus switches and two routers interconnected within the topology.
• All activity will be done on the Nexus P01SW01 switch which has the hostname CiscoLive_N7K.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 2 of 19
Cisco dCloud
Get Started
BEFORE PRESENTING
We strongly recommend that you go through this document and work with an active session before presenting in front of a live
audience. This will allow you to become familiar with the structure of the document and content.
1. Browse to dcloud.cisco.com, select the location closest to you, and log in with your Cisco.com credentials.
4. Verify that the status of your session is Active in My Dashboard > My Sessions.
IMPORTANT: An Active session does not indicate that the demo is fully launched. It may take up to 10 minutes for this
demonstration to fully launch AFTER the simulation is MANUALLY started within VIRL.
6. For best performance, connect to the workstation with Cisco AnyConnect VPN [Show Me How] and the local RDP client on
your laptop [Show Me How]
NOTE: You can also connect to the workstation using the Cisco dCloud Remote Desktop client [Show Me How]. The dCloud
Remote Desktop client works best for accessing an active session with minimal interaction.
7. From the workstation desktop, double-click the VMMaestro icon ( ) to launch VMMaestro.
• Once VMMaestro launches, it should have a topology open with the name ‘dcloud_lab_kt_pod01-05.virl’ under ‘My
Topologies’.
• The Nexus switch P01SW01 will not be available until the demonstration has MANUALLY been launched, and completed
all launch processes. To MANUALLY launch the demonstration click the green play button.
• In the ‘Simulations’ tab four devices will be listed and in the [ACTIVE] state once the simulation has successfully
launched.
• Right click the P01SW01[ACTIVE] device, move the mouse cursor over the ‘Telnet’ menu option, and click the remote
access information for the console port.
• Alternatively, you can also directly connect to the Nexus switch using a Telnet application from your laptop or desktop
computer, using the console IP address 198.18.134.1 with the associated port_number.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 3 of 19
Cisco dCloud
• The port number for the console session that you see when you right click on the P01SW01[ACTIVE] device.
• All activities will only be done on the Nexus switch P01SW01, which has a node name of CiscoLive_N7K. To login to the
Nexus 7000 use the username: guest and the password: guest. You should be at the prompt: CiscoLive_N7K#
NOTE: When using telnet to the Nexus 7000 P01SW01 device omit the colon (e.g. telnet 198.18.134.1 17000). Also omit the colon
if entering the port number into your terminal application.
show hostname
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 4 of 19
Cisco dCloud
Steps
In this scenario, you will access the Python interpreter on the Nexus switch and use some of the available APIs.
CiscoLive_N7K# python
CiscoLive_N7K# >>>
NOTE: The expected prompt will be in orange, and the output will be in red.
2. Note the prompt change to ‘>>>”. Enter ‘exit’ from the interpreter to return to the enable prompt ‘#’.
CiscoLive_N7K#
IMPORTANT: When you exit the interpreter all code is lost. Be aware of the prompt changes during the exercises!!!
1. Enter ‘help()’, this calls the help function. Read the screen output for additional information on the function.
<Truncated output>
<Truncated output>
CiscoLive_N7K# help>
3. Enter ‘cisco’. View the contents of the cisco module. We will use the cisco module to access the CLI API.
NAME
<Truncated output>
CiscoLive_N7K# help>
4. Hit the ‘Enter’ key twice at the help> prompt to exit help.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 5 of 19
Cisco dCloud
CiscoLive_N7K# help>
CiscoLive_N7K# >>>
5. If you want to ask for help on a particular object directly from the interpreter, you can enter: ‘help(“object”)
<Truncated output>
6. Omitting the double-quotes will fail. Importing the module is required to not use double-quotes.
<Truncated output>
<Truncated output>
1. Execute CLI commands from the interpreter using the command ‘cli()’ API. Notice the output contains control/special
characters.
'CiscoLive_N7K \n'
<Truncated output>
2. The ‘clip()’ API outputs directly to stdout and returns nothing to Python, but is more readable.
CiscoLive_N7K
<Truncated output>
3. For CLI commands that support XML the ‘clid()’ API returns outputs as a Python dictionary.
{'hostname': 'CiscoLive_N7K'}
CiscoLive_N7K# >>>
4. Assign the ‘clid()’ output to the variable called hostname. Check the type of the object. Note that ‘hostname’ is a dict. Extract
the value of the key ‘hostname’ from the variable hostname.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 6 of 19
Cisco dCloud
<type 'dict'>
'CiscoLive_N7K'
5. Change the hostname of the switch. Separate multiple commands with a semi-colon within double quotes. Notice the prompt
changed to config-mode.
''
somethingelse(config)#
somethingelse(config)# >>>
''
CiscoLive_N7K(config)#
CiscoLive_N7K(config)# >>>
''
CiscoLive_N7K# >>>
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 7 of 19
Cisco dCloud
Steps
In this section, you will use Python variables, use Python script mode, import modules, define functions, and create a for-loop.
1. Concatenate variables var1 and var2 and change var2 to upper case.
1. Create a Python script file and write it to the default script directory ‘bootflash:scripts’. All scripts must be stored in
bootflash:scripts or in a sub-directory. From the enable prompt view the contents of the script using the show file … command.
From the enable prompt run the script using the ‘source’ command.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 8 of 19
Cisco dCloud
314.1592653589793
CiscoLive_N7K# >>>
2. Create a function called ‘areaCircle’ that will calculate the area of a circle when given the argument radius. The interpreter
prompt ‘…’ indicates a code block and you must indent with 4 spaces before typing any code, after the function header!!!
NOTE: Errors can occur if you do not have the correct indentation.
CiscoLive_N7K# ...
CiscoLive_N7K# >>>
CiscoLive_N7K# ...
314.1592653589793
CiscoLive_N7K# >>>
1. Create a for-loop that calculates the area of a circle for 3 different random radius. Your integer values will be different.
CiscoLive_N7K# ...
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 9 of 19
Cisco dCloud
CiscoLive_N7K# ...
201.06192982974676
113.09733552923255
201.06192982974676
1. From now on the interpreter prompt and enable prompt will be abbreviated. Create a function that converts an IPv4 decimal
address to a IPv6 hexadecimal address. NOTE: Remember indentation and watch for typos e.g. spaces, spelling and special
characters. Python is an unforgiving language.
... hexadecimal = []
... hexadecimal.append(hex(int(item))[2:].zfill(2))
... ipv6address.append(":" + i + j)
...
>>>
2001::0a07:0001
1. A convertipv4.py script file was created in the bootflash:scripts directory. The convertipv4.py script file can be imported as a
module and used in your code. You can call the method ‘getIPv6’ to convert an IPv4 address.
>>> getIPv6("10.7.0.1")
'2001::0a07:0001'
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 10 of 19
Cisco dCloud
Steps
1. Using the CLI API we will capture the output of the CDP table. We only need the specific neighbor information. So we will start
to remove unnecessary lines of data. When for-loop completes it will display which items were popped. The 1st six lines are
removed.
[]
>>> pprint.pprint(cdp_table)
>>> cdp_table[0]
>>> cdp_table[-1]
>>> cdp_table.pop(0)
...
>>>
2. Create cdp_list1, using list comprehension, to add each cdp_table line as an individual list within cdp_list1. Use split() to split
items within each list. Create cdp_list2 and only add cdp_list1 items that are not empty.
>>> pprint.pprint(cdp_table)
>>> pprint.pprint(cdp_list1)
>>> pprint.pprint(cdp_list2)
[['R101 '],
['N7K_2(TB3EB2EEBDB)'],
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 11 of 19
Cisco dCloud
['N7K_2(TB3EB2EEBDB)'],
['N7K_2(TB3EB2EEBDB)'],
['N7K_2(TB3EB2EEBDB)'],
3. CDP neighbors with large names need to be inserted in the next line and then removed. Also notice there is a list starting with
['Total'...] at the end of the output which must be removed.
... if len(item) == 1:
... cdp_list2.pop(item_location)
... cdp_list2.pop(item_location)
...
>>>
>>> pprint.pprint(cdp_list2)
4. Now add each CDP neighbor into the dictionary cdp_dict as a main key of nested dictionary key:values. Add the local/remote
interfaces as the nested dictionary key:values. Make the nested dictionary values of type list, to allow for multiple interfaces to
be added.
... cdp_dict[item[0]] = {}
... cdp_dict[item[0]]['local_intf'].append(item[1])
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 12 of 19
Cisco dCloud
... cdp_dict[item[0]]['remote_intf'].append(item[-1])
...
>>>
5. Now configure a description on all local interfaces, identifying the neighbor and its connected interface. Use the cdp_dict
key:value pairs to create the description syntax, and configure using the cli() API.
... cli("end")
...
>>>
interface Ethernet2/1
description local intf Eth2/1 connected to R101 's remote intf Gig0/1 (via screen scraping)
interface Ethernet2/2
description local intf Eth2/2 connected to N7K_2(TB3EB2EEBDB)'s remote intf Eth2/2 (via
screen scraping)
interface Ethernet2/3
description local intf Eth2/3 connected to N7K_2(TB3EB2EEBDB)'s remote intf Eth2/3 (via
screen scraping)
interface Ethernet2/4
description local intf Eth2/4 connected to N7K_2(TB3EB2EEBDB)'s remote intf Eth2/4 (via
screen scraping)
interface Ethernet2/5
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 13 of 19
Cisco dCloud
description local intf Eth2/5 connected to N7K_2(TB3EB2EEBDB)'s remote intf Eth2/5 (via
screen scraping)
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 14 of 19
Cisco dCloud
Steps
1. Review the XML tree data structure and the hierarchy and element names. The XML namespace is assigned to xmlns.
[]
2. Using the XML namespace and an element as a starting point, you can iterate and extract specific data. The CDP neighbor
table info is assigned specific XML element names such as ‘device_id’, ‘intf_id’ and ‘port_id’.
... cdp_dict[neighbor] = {}
... cdp_dict[neighbor]['local_intf'].append(local_intf)
... cdp_dict[neighbor]['remote_intf'].append(local_intf)
...
>>>
3. Assign the neighbor and associated local and remote interfaces into a nested dictionary.
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 15 of 19
Cisco dCloud
... cli("end")
...
>>>
interface Ethernet2/1
description local intf Ethernet2/1 connected to R101 's remote intf GigabitEthernet0/1 (via
XML)
interface Ethernet2/2
interface Ethernet2/3
interface Ethernet2/4
interface Ethernet2/5
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 16 of 19
Cisco dCloud
Steps
1. Import the convertipv4 module to use the getIPv6 method. Review the XML tree data structure and the hierarchy and element
names. The XML namespace is assigned to xmlns.
[]
2. Using the XML namespace and an element as a starting point, you can iterate and extract specific data. Use the getIPv6
method to convert the IPv4 address.
... intf_brief_dict[intf_name] = {}
...
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 17 of 19
Cisco dCloud
3. Now configure IPv6 address on all IPv4 interfaces, using the cli() API. The loopback701 and Eth2/1-5 interfaces should all
have IPv6 addresses configured. The IPv6 routing table now has directly attached routes, and is learning the Loopback702
ipv6 address on N7K_2 via ospfv3.
... else:
... cli("end")
...
4. A bgpv6_configlet.txt file that contains the required BGPv6 configuration was created in the bootflash:scripts directory. Open
and read the file and put into a list.
>>> type(bgpv6_config)
5. Change the interpreter prompt into configuration mode. Iterate over each line in the list and configure the BGPv6 protocol.
Confirm BGPv6 peering is up and you are receiving ipv6 routes.
(config) # ...
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 18 of 19
Cisco dCloud
(config) # ...
>>>
© 2015 Cisco and/or its affiliates. All rights reserved. This document is Cisco Public Information. Page 19 of 19