networktocode / ntc-templates

TextFSM templates for parsing show commands of network devices
https://ntc-templates.readthedocs.io/
Other
1.12k stars 730 forks source link

Cisco IOS "dir bootflash:" not returning minimim values expected with no files in flash #1237

Closed IvanShires closed 4 months ago

IvanShires commented 1 year ago
switch_name#
switch_name#dir bootflash:
Directory of bootflash:/

No files in directory

60817408 bytes total (16626160 bytes free)

Using the cisco_ios_dir.textfsm template, I am unable to return the "bytes free" field (or bytes total) when there are no files present on the device.

Code:

def get_switch_freespace(info):
    with open("text-fsm-templates/cisco_ios_dir.textfsm") as f:
        template = textfsm.TextFSM(f)
    fsm_results = template.ParseText(info)
    return fsm_results

data = """

switch_name#
switch_name#dir bootflash:
Directory of bootflash:/

No files in directory

60817408 bytes total (16626160 bytes free)

"""

print(get_switch_freespace(data))

Which outputs: []

Template file:

Value Filldown FILE_SYSTEM (\S+)
Value ID (\d+)
Value PERMISSIONS (.+?)
Value SIZE (\d+)
Value Fillup TOTAL_SIZE (\d+)
Value Fillup TOTAL_FREE (\d+)
Value DATE_TIME (.+?)
Value NAME (\S+)

Start
  ^\s*${ID}\s+${PERMISSIONS}\s+${SIZE}\s+${DATE_TIME}\s+${NAME}\s*$$ -> Record
  ^Directory of\s+${FILE_SYSTEM}
  ^${TOTAL_SIZE}\s+\S+\s+\S+\s\(${TOTAL_FREE} bytes free\)
  ^Load\s+for\s+
...
  ^Time\s+source\s+is

EOF

In my head, this should work.. But I also don't know if there is something I am missing, I am not familiar enough with Regex (specifically with the value parameters) to confidently diagnose this..

show version output:

switch_name#sh ver
Cisco IOS Software, Catalyst 4500 L3 Switch Software (cat4500-IPBASEK9-M), Version 12.2(31)SGA, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2006 by Cisco Systems, Inc.
Compiled Fri 08-Sep-06 10:19 by ccai
Image text-base: 0x10000000, data-base: 0x115C42A4

ROM: 12.2(20r)EW1
Dagobah Revision 225, Swamp Revision 4

switch_name uptime is 19 hours, 34 minutes
System returned to ROM by power-on
System image file is "bootflash:cat4500-ipbasek9-mz.122-31.SGA.bin"

This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.

cisco WS-C4948 (MPC8245) processor (revision 0) with 262144K bytes of memory.
Processor board ID XYZ12345678
MPC8245 CPU at 266Mhz, Fixed Module
Last reset from PowerUp
1 Virtual Ethernet interface
48 Gigabit Ethernet interfaces
511K bytes of non-volatile configuration memory.

Configuration register is 0x2102
IvanShires commented 1 year ago

Switch that has files in the bootflash:

data = """

switch2_name#
switch2_name#dir bootflash:
Directory of bootflash:/

    1  -rwx    19476896   Jan 3 2018 09:15:49 +00:00  cat4500-entservicesk9-mz.150-2.SG10.bin
    2  -rw-        1501   May 6 2022 11:18:08 +00:00  network-confg

60817408 bytes total (41338752 bytes free)

"""

get_switch_freespace(data)

Produces expected output:

[['bootflash:/',
  '1',
  '-rwx',
  '19476896',
  '60817408',
  '41338752',
  'Jan 3 2018 09:15:49 +00:00',
  'cat4500-entservicesk9-mz.150-2.SG10.bin'],
 ['bootflash:/',
  '2',
  '-rw-',
  '1501',
  '60817408',
  '41338752',
  'May 6 2022 11:18:08 +00:00',
  'network-confg']]
mjbear commented 4 months ago

@IvanShires The "file" line with Record is the key to why this doesn't work with any files present on flash.

If that Record line is not matched then nothing gets captured.

I did some experimentation and I can get things to capture a new entry for the TOTAL_SIZE and TOTAL_FREE, but it creates a new entry with a blank file name. This also occurs in the output for a switch that has files.

:question: Is this allowable?

Thoughts? @jvanderaa @jmcgill298

Update: :bulb:

  1. How common of an occurrence do we think this is to have a running switch with no image(s) on flash?
jmcgill298 commented 4 months ago

We should probably use Continue.Record for Directory of

mjbear commented 4 months ago

We should probably use Continue.Record for Directory of

@jmcgill298 This does indeed help. The side effect is that there's an extra dictionary with a blank file name in the output which does have files. (example below)

If this isn't a big drawback, I have the changes in a local branch ready to go.

---
parsed_sample:
  - date_time: ""
    file_system: "bootflash:/"
    id: ""
    name: ""
    permissions: ""
    size: ""
    total_free: "6612774912"
    total_size: "7835619328"
  - date_time: "Mar 2 2015 08:46:31 +00:00"
    file_system: "bootflash:/"
    id: "11"
    name: "lost+found"
    permissions: "drwx"
    size: "16384"
    total_free: "6612774912"
    total_size: "7835619328"
  - date_time: "Mar 2 2015 08:47:35 +00:00"
    file_system: "bootflash:/"
    id: "681409"
    name: ".super.iso.dir"
    permissions: "drwx"
    size: "4096"
    total_free: "6612774912"
    total_size: "7835619328"
... snipped ...
jmcgill298 commented 4 months ago

@mjbear that is because you are doing Filldown, you can either make a non-filldown Required, or end the file with EOF to override the default EOF -> Record

mjbear commented 4 months ago

Hmmm. :thinking:

This textfsm template already has EOF at the end.

For raw dir output with no files, the three values that will be present are FILE_SYSTEM, TOTAL_SIZE, and TOTAL_FREE. The first one is Filldown and the latter two are Fillup.

@jmcgill298 I appreciate the thoughts and guidance! Thank you!