Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cisco_nxos_show_interface_transciever parsing issue #1534

Open
Tes3awy opened this issue Oct 1, 2023 · 5 comments
Open

cisco_nxos_show_interface_transciever parsing issue #1534

Tes3awy opened this issue Oct 1, 2023 · 5 comments

Comments

@Tes3awy
Copy link

Tes3awy commented Oct 1, 2023

ISSUE TYPE
  • Template Issue with error and raw data
  • Bug Report with the library
TEMPLATE USING
Value INTERFACE (\S+/\S+)
Value MANUFACTURER (\S+)
Value TYPE (.+)
Value SERIAL (\S+)
Value PART_NUMBER (\S+)

Start
  ^${INTERFACE}
  ^\s+transceiver is not -> Clear
  ^\s+type\s+is(\s+${TYPE})?
  ^\s+name\s+is(\s+${MANUFACTURER})?
  ^\s+part\s+number\s+is(\s+${PART_NUMBER})?
  ^\s+serial\s+number\s+is(\s+${SERIAL})?
  ^\s+transceiver\s+
  ^\s+nominal\s+
  ^\s+revision\s+
  ^\s+Link\s+
  ^\s+cable\s+type\s+is
  ^\s+cisco\s+id
  ^\s+cisco\s+extended
  ^\s+cisco\s+part\s+number
  ^\s+cisco\s+product\s+id
  ^\s+cisco\s+version\s+id
  ^\s*$$ -> Record
  ^. -> Error
SAMPLE COMMAND OUTPUT
Ethernet1/1
    transceiver is not applicable

Ethernet1/2
    transceiver is not applicable

Ethernet1/3
    transceiver is not applicable

Ethernet1/4
    transceiver is not applicable

Ethernet1/5
    transceiver is not applicable

Ethernet1/6
    transceiver is not applicable

Ethernet1/7
    transceiver is not applicable

Ethernet1/8
    transceiver is not applicable

Ethernet1/9
    transceiver is not applicable

Ethernet1/10
    transceiver is not applicable

Ethernet1/11
    transceiver is not applicable

Ethernet1/12
    transceiver is not applicable

Ethernet1/13
    transceiver is not applicable

Ethernet1/14
    transceiver is not applicable

Ethernet1/15
    transceiver is not applicable

Ethernet1/16
    transceiver is not applicable

Ethernet1/17
    transceiver is not applicable

Ethernet1/18
    transceiver is not applicable

Ethernet1/19
    transceiver is not applicable

Ethernet1/20
    transceiver is not applicable

Ethernet1/21
    transceiver is not applicable

Ethernet1/22
    transceiver is not applicable

Ethernet1/23
    transceiver is not applicable

Ethernet1/24
    transceiver is not applicable

Ethernet1/25
    transceiver is not applicable

Ethernet1/26
    transceiver is not applicable

Ethernet1/27
    transceiver is not applicable

Ethernet1/28
    transceiver is not applicable

Ethernet1/29
    transceiver is not applicable

Ethernet1/30
    transceiver is not applicable

Ethernet1/31
    transceiver is not applicable

Ethernet1/32
    transceiver is not applicable

Ethernet1/33
    transceiver is not applicable

Ethernet1/34
    transceiver is not applicable

Ethernet1/35
    transceiver is not applicable

Ethernet1/36
    transceiver is not applicable

Ethernet1/37
    transceiver is not applicable

Ethernet1/38
    transceiver is not applicable

Ethernet1/39
    transceiver is not applicable

Ethernet1/40
    transceiver is not applicable

Ethernet1/41
    transceiver is not applicable

Ethernet1/42
    transceiver is not applicable

Ethernet1/43
    transceiver is not applicable

Ethernet1/44
    transceiver is not applicable

Ethernet1/45
    transceiver is not applicable

Ethernet1/46
    transceiver is not applicable

Ethernet1/47
    transceiver is not applicable

Ethernet1/48
    transceiver is not applicable

Ethernet1/49
    transceiver is not applicable

Ethernet1/50
    transceiver is not applicable

Ethernet1/51
    transceiver is not applicable

Ethernet1/52
    transceiver is not applicable

Ethernet1/53
    transceiver is not applicable

Ethernet1/54
    transceiver is not applicable

Ethernet1/55
    transceiver is not applicable

Ethernet1/56
    transceiver is not applicable

Ethernet1/57
    transceiver is not applicable

Ethernet1/58
    transceiver is not applicable

Ethernet1/59
    transceiver is not applicable

Ethernet1/60
    transceiver is not applicable

Ethernet1/61
    transceiver is not applicable

Ethernet1/62
    transceiver is not applicable

Ethernet1/63
    transceiver is not applicable

Ethernet1/64
    transceiver is not applicable

SUMMARY

Cannot parse Nexus 9000 show interface transciever output.

STEPS TO REPRODUCE
from pprint import pprint

from netmiko import ConnectHandler
from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException

device = {
    "device_type": "cisco_nxos",
    "ip": "sbx-nxos-mgmt.cisco.com",
    "username": "admin",
    "password": "Admin_1234!",
    "secret": "",
    "fast_cli": False,
}

try:
    print(f"Trying {device['ip']}...", end="\r")
    conn = ConnectHandler(**device)
    print(f"Connected to {conn.host}:{conn.port}")
    if not conn.check_enable_mode():
        conn.enable()
    transceivers = conn.send_command(
        command_string="show interface transceiver", use_textfsm=True
    )
except (
    NetmikoAuthenticationException,
    NetmikoTimeoutException,
    ConnectionResetError,
    TimeoutError,
) as e:
    print(f"Failed to connect to {device['ip']} due to {type(e).__name__}: {e}")
else:
    conn.disconnect()
    pprint(transceivers)
EXPECTED RESULTS
ACTUAL RESULTS
Ethernet1/1
    transceiver is not applicable

Ethernet1/2
    transceiver is not applicable

Ethernet1/3
    transceiver is not applicable

Ethernet1/4
    transceiver is not applicable

Ethernet1/5
    transceiver is not applicable

Ethernet1/6
    transceiver is not applicable

Ethernet1/7
    transceiver is not applicable

Ethernet1/8
    transceiver is not applicable

Ethernet1/9
    transceiver is not applicable

Ethernet1/10
    transceiver is not applicable

Ethernet1/11
    transceiver is not applicable

Ethernet1/12
    transceiver is not applicable

Ethernet1/13
    transceiver is not applicable

Ethernet1/14
    transceiver is not applicable

Ethernet1/15
    transceiver is not applicable

Ethernet1/16
    transceiver is not applicable

Ethernet1/17
    transceiver is not applicable

Ethernet1/18
    transceiver is not applicable

Ethernet1/19
    transceiver is not applicable

Ethernet1/20
    transceiver is not applicable

Ethernet1/21
    transceiver is not applicable

Ethernet1/22
    transceiver is not applicable

Ethernet1/23
    transceiver is not applicable

Ethernet1/24
    transceiver is not applicable

Ethernet1/25
    transceiver is not applicable

Ethernet1/26
    transceiver is not applicable

Ethernet1/27
    transceiver is not applicable

Ethernet1/28
    transceiver is not applicable

Ethernet1/29
    transceiver is not applicable

Ethernet1/30
    transceiver is not applicable

Ethernet1/31
    transceiver is not applicable

Ethernet1/32
    transceiver is not applicable

Ethernet1/33
    transceiver is not applicable

Ethernet1/34
    transceiver is not applicable

Ethernet1/35
    transceiver is not applicable

Ethernet1/36
    transceiver is not applicable

Ethernet1/37
    transceiver is not applicable

Ethernet1/38
    transceiver is not applicable

Ethernet1/39
    transceiver is not applicable

Ethernet1/40
    transceiver is not applicable

Ethernet1/41
    transceiver is not applicable

Ethernet1/42
    transceiver is not applicable

Ethernet1/43
    transceiver is not applicable

Ethernet1/44
    transceiver is not applicable

Ethernet1/45
    transceiver is not applicable

Ethernet1/46
    transceiver is not applicable

Ethernet1/47
    transceiver is not applicable

Ethernet1/48
    transceiver is not applicable

Ethernet1/49
    transceiver is not applicable

Ethernet1/50
    transceiver is not applicable

Ethernet1/51
    transceiver is not applicable

Ethernet1/52
    transceiver is not applicable

Ethernet1/53
    transceiver is not applicable

Ethernet1/54
    transceiver is not applicable

Ethernet1/55
    transceiver is not applicable

Ethernet1/56
    transceiver is not applicable

Ethernet1/57
    transceiver is not applicable

Ethernet1/58
    transceiver is not applicable

Ethernet1/59
    transceiver is not applicable

Ethernet1/60
    transceiver is not applicable

Ethernet1/61
    transceiver is not applicable

Ethernet1/62
    transceiver is not applicable

Ethernet1/63
    transceiver is not applicable

Ethernet1/64
    transceiver is not applicable

@collin-wicker
Copy link
Contributor

I have replicated the steps you describe and everything appears to be working correctly with the parsing to me. The transceiver data that you are getting back from sbx-nxos-mgmt.cisco.com is empty ("transciever is not applicable"). When the cisco_nxos_show_interface_transciever parser is applied to the output stored in your variable called "transcievers" it returns an empty list, which is correct as there is no actual transciever data in the output.

If you look at the second line after the Start state (^\s+transceiver is not -> Clear) this line is accounting for the potential situation where the transceiver state for a particular interface begins with is "transceiver is not" (as the output in your transcivers variable does) and clears any previously captured parsing data, moves onto parsing the next line, and does not record any values for that particular line of output, as it does not contain any applicable transciever data.

In short, this does not appear to be a bug with the parsing template but is the intended result for the command_output ("transciver" variable data) that you are feeding into it

@Tes3awy
Copy link
Author

Tes3awy commented Nov 18, 2023

Hi @collin-wicker, thanks for you explanation. I get your point regarding the issue. But, I want to mention that when I use the NX-API feature (with the below code), I can successfully get the parsed output.

Requires feature nxapi to be enabled on the Nexus switch. (Ref: Enable the NX-API nxapi feature)

I know that ntc-templates (textfsm) and Cisco (most probably Genie) do the parsing using different parsers, but I think even if there are no SFPs inserted in any of the interfaces, I should still get an output from ntc-templates.

# Imports
import csv
from datetime import date
from getpass import getpass
from pprint import pprint

import requests
import urllib3
from urllib3.exceptions import InsecureRequestWarning

urllib3.disable_warnings(InsecureRequestWarning)

# Inputs
host = input("Host [sbx-nxos-mgmt.cisco.com]: ") or "sbx-nxos-mgmt.cisco.com"
usr = input("Username [admin]: ") or "admin"
pwd = getpass("Password: ") or "Admin_1234!"

data = {
    "ins_api": {
        "version": "1.0",
        "type": "cli_show",
        "chunk": "0",
        "sid": "1",
        "input": "show interface transceiver",  # the command
        "output_format": "json",  # or xml
    }
}

sess = requests.Session()
try:
    print(f"Trying {host}...", end="\r")
    # POST: Request
    r = sess.post(
        url=f"https://{host}:443/ins", auth=(usr, pwd), json=data, verify=False
    )
    r.raise_for_status()
except requests.HTTPError as e:
    raise SystemExit(e) from e
else:
    print(f"Connected to {host} successfully")
    interfaces = (
        r.json()
        .get("ins_api")
        .get("outputs")
        .get("output")
        .get("body")
        .get("TABLE_interface")
        .get("ROW_interface")
    )

    pprint(interfaces)

    # Create CSV file
    with open(
        file=f"{host}-transceivers_{date.today()}.csv", mode="wt", newline=""
    ) as csvfile:
        csvwriter = csv.DictWriter(csvfile, fieldnames=["interface", "sfp"])
        csvwriter.writerows(interfaces)
    print(f"Created {csvfile.name} file successfully")

PPrint output

[{'interface': 'Ethernet1/1', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/2', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/3', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/4', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/5', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/6', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/7', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/8', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/9', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/10', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/11', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/12', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/13', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/14', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/15', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/16', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/17', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/18', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/19', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/20', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/21', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/22', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/23', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/24', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/25', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/26', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/27', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/28', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/29', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/30', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/31', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/32', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/33', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/34', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/35', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/36', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/37', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/38', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/39', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/40', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/41', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/42', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/43', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/44', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/45', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/46', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/47', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/48', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/49', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/50', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/51', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/52', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/53', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/54', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/55', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/56', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/57', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/58', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/59', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/60', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/61', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/62', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/63', 'sfp': 'not applicable'},
 {'interface': 'Ethernet1/64', 'sfp': 'not applicable'}]

@collin-wicker
Copy link
Contributor

@Tes3awy I see what you're saying regarding the above output you received from the NX-API. This seems like a logical way to parse the "show interface transceiver" command output and could certainly be achieved with NTC-Templates parser. I will defer to the repo maintainers on this.

@jvanderaa, @itdependsnetworks, @jmcgill298 Do you all have an opinion as to the format of the data being returned from ntc-templates in this instance. It is currently returning an empty list due to the "transceiver is not applicable" line but @Tes3awy is suggesting it return something similar to the directly above output from the NX-API.

@jmcgill298
Copy link
Contributor

Personally, I would expect an empty list. But I'm not really concerned either way

@SaschaSchwarzK
Copy link
Contributor

I just submitted a pull request #1614 as I was facing another issue that did throw an error with the vendor ID.
I have added a extra field for the transceiver status that could be used to filter out just the results you need. If you just want to have present SFPs filter on "present".
But it would also allow you to identify 10GBASE-T ports on N9k or 100/1000BASE-T on N2K as "not applicable" or empty ports as "not present".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants