These examples show how to use the methods of the Cisco Nexus 9000 Series Python SDK. The examples assume that the scripts are stored in the bootflash:scripts directory of the switch.
You can display the script source using the show file CLI command, as in this example:
switch# show file bootflash:scripts/deltaCounters.py
Example: deltaCounters.py
The deltaCounters.py script shows the increment in receive and transmit counters for a specified interface over a period.
The usage is:
deltaCounters.py interface seconds count
For example, deltaCounters.py Ethernet1/1 10 5 displays the counters for the Ethernet1/1 interface every 10 seconds for 5 periods.
Source
#!/isan/bin/python
from cli import *
import sys, time
ifName = sys.argv[1]
delay = float(sys.argv[2])
count = int(sys.argv[3])
cmd = 'show interface ' + ifName + ' counters'
out = json.loads(clid(cmd))
rxuc = int(out['TABLE_rx_counters']['ROW_rx_counters'][0]['eth_inucast'])
rxmc = int(out['TABLE_rx_counters']['ROW_rx_counters'][1]['eth_inmcast'])
rxbc = int(out['TABLE_rx_counters']['ROW_rx_counters'][1]['eth_inbcast'])
txuc = int(out['TABLE_tx_counters']['ROW_tx_counters'][0]['eth_outucast'])
txmc = int(out['TABLE_tx_counters']['ROW_tx_counters'][1]['eth_outmcast'])
txbc = int(out['TABLE_tx_counters']['ROW_tx_counters'][1]['eth_outbcast'])
print 'row rx_ucast rx_mcast rx_bcast tx_ucast tx_mcast tx_bcast'
print '========================================================='
print ' %8d %8d %8d %8d %8d %8d' % (rxuc, rxmc, rxbc, txuc, txmc, txbc)
print '========================================================='
i = 0
while (i < count):
time.sleep(delay)
out = json.loads(clid(cmd))
rxucNew = int(out['TABLE_rx_counters']['ROW_rx_counters'][0]['eth_inucast'])
rxmcNew = int(out['TABLE_rx_counters']['ROW_rx_counters'][1]['eth_inmcast'])
rxbcNew = int(out['TABLE_rx_counters']['ROW_rx_counters'][1]['eth_inbcast'])
txucNew = int(out['TABLE_tx_counters']['ROW_tx_counters'][0]['eth_outucast'])
txmcNew = int(out['TABLE_tx_counters']['ROW_tx_counters'][1]['eth_outmcast'])
txbcNew = int(out['TABLE_tx_counters']['ROW_tx_counters'][1]['eth_outbcast'])
i += 1
print '%-3d %8d %8d %8d %8d %8d %8d' % \
(i, rxucNew - rxuc, rxmcNew - rxmc, rxbcNew - rxbc, txucNew - txuc, txmcNew - txmc, txbcNew - txbc)
Output
switch# python bootflash:scripts/deltaCounters.py Ethernet1/1 1 5
row rx_ucast rx_mcast rx_bcast tx_ucast tx_mcast tx_bcast
=========================================================
0 791 1 0 212739 0
=========================================================
1 0 0 0 0 26 0
2 0 0 0 0 27 0
3 0 1 0 0 54 0
4 0 1 0 0 55 0
5 0 1 0 0 81 0
switch#
Example: reloadin.py
The reloadin.py script replicates the IOS reload in command to schedule reloads using the NX-OS scheduler feature.
The usage is:
reloadin.py minutes [ save ] | cancel
To schedule a switch reload after a number of minutes, execute the command as reloadin.py minutes, as in this example:
source reloadin.py 20
To save the configuration before reloading, execute the command as reloadin.py minutes save, as in this example:
source reloadin.py 20 save
To cancel a pending reload after you have issued a reload command, execute the command as reloadin.py cancel, as in this example:
source reloadin.py cancel
Note: This script has not been tested with a period spanning a day change (or example, if the current time is 23:55 and the reload period is 20 minutes).
Source
#!/isan/bin/python
import datetime
from cli import cli
import sys
numArg = len(sys.argv)
# check if cancel
if sys.argv[1] == "cancel":
schedCLIjob = 'conf t ; no scheduler job name reloadinCommand5657373'
schedCLItime = 'conf t ; no scheduler schedule name reloadinCommand5657373'
print "Canceling reload"
# check number of argvs and if first one is an integer
elif numArg <= 3:
try:
requestTime = int(sys.argv[1])
except:
print "Enter a integer for time"
sys.exit() #bail out if input is wrong
now = datetime.datetime.now()
actionTime = now + datetime.timedelta(minutes = requestTime)
reloadTime = str(actionTime)
reloadTime=reloadTime[11:-10]
schedCLIjob = 'conf t ; scheduler job name reloadinCommand5657373 ; reload ; exit'
schedCLItime = 'conf t ; scheduler schedule name reloadinCommand5657373 ; time start ' + reloadTime + ' repeat 48:00 ; end '
if numArg == 3 and sys.argv[2] == "save".lower():
cli('copy running-config startup-config')
print "Saving config before reload"
print "current time on the switch is " + str(now)
print "reload scheduled at " + reloadTime
# run the CLI
cli('conf t ; feature scheduler')
try:
cli(schedCLIjob)
except:
print "operation failed..did you cancel a job that was not there?"
sys.exit()
cli(schedCLItime)
print "Operation success"
Output
switch# source reloadin 60
current time on the switch is 2015-08-30 16:47:15.992211
reload scheduled at 17:47
Operation success
switch# source reloadin cancel
Canceling reload
Operation success