Network OS Administration Guide

Supporting Network OS 6.0.1a

Part Number: 53-1003768-04

Script illustrating the .get_output function (get_output.py)

The .get_output function returns—as a list—the output of the Network OS CLI commands assigned to a Python object.

Script (get_output.py)

This script displays a list of the following information:
  • Number of RBridges
  • World-wide name (WWN) addresses
  • IP addresses of the management interfaces
  • The VCS status
  • The fabric status
  • Device hostnames
NOTE
Lines beginning with # are annotations.
#Required in all scripts for NOS:
from CLI import CLI

# Import the Python Regular Expressions (re) module:
import re

# Create Python objects:
rbridges = []
wwns = []
mgmt_ip_addresses = []
vcs_status = []
fabric_status = []
hostnames = []

# Runs show vcs, and assigns this NOS command to a Python object named cmd_show_vcs. 
# The optional "False" keyword suppresses stdout output: 
cmd_show_vcs = CLI("show vcs", False)

# As a list, assign the result of show vcs to a Python object named output:
output = cmd_show_vcs.get_output()

for line in output:
    found = re.search(r'^(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$', line, re.M)

    if found:
        rbridges.append(found.group(1))
        wwns.append(found.group(2).strip('>*'))
        mgmt_ip_addresses.append(found.group(3))
        vcs_status.append(found.group(4))
        fabric_status.append(found.group(5))
        hostnames.append(found.group(6))

print('rbridges: ', rbridges)
print('wwns: ', wwns)
print('mgmt_ip_addresses: ', mgmt_ip_addresses)
print('vcs_status: ', vcs_status)
print('fabric_status: ', fabric_status)
print('hostnames: ', hostnames)

Run-log (get_output.py)

A log upon running get_output.py was as follows:

device# python get_output.py
rbridges:  ['2']
wwns:  ['10:00:00:05:33:65:ED:F0']
mgmt_ip_addresses:  ['10.24.81.101']
vcs_status:  ['Online']
fabric_status:  ['Online']
hostnames:  ['sw0']