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

how to use this template can any once give 1 example and its result for more and better understanding #193

Closed
yusufq8 opened this issue May 1, 2018 · 4 comments

Comments

@yusufq8
Copy link

yusufq8 commented May 1, 2018

ISSUE TYPE
  • Template Creation Help
TEMPLATE USING

SAMPLE COMMAND OUTPUT

SUMMARY
@jmcgill298
Copy link
Contributor

@yusufq8 what are you asking? How to create a new template? How to use TextFSM? Something else?

@yusufq8
Copy link
Author

yusufq8 commented May 11, 2018 via email

@cldeluna
Copy link

Hi @yusufq8,

Here is a simple example. First you need to select the template that corresponds to the data you want (the information you want to parse out of your content) and then you need content to parse (you can get this in a variety of ways but we will keep it simple and assume I have a text file of show commands that I need to parse to get the device hardware information and software version).

For that I looked at the existing templates and the cisco_ios_show_version.template has what I need, VERSION and HARDWARE (which will return a list) and my file of show commands includes the output of the "show version" command.

Here are the two files I will work with

  • the textFSM template file
    ntc-templates/templates/cisco_ios_show_version.template

  • the content file
    lab-swtich-show-cmds.txt

for simplicity I've put them both in a temp directory and I will launch the python interpreter from there so we can work real time. I also list the modules that I have in the virtual environment. The only one you need for this example is textfsm.

my working directory and files

(cat_scripts) Claudias-iMac:textfsm-example claudia$ pwd
/Users/claudia/Downloads/textfsm-example
(cat_scripts) Claudias-iMac:textfsm-example claudia$ ls
cisco_ios_show_version.template	lab-swtich-show-cmds.txt
(cat_scripts) Claudias-iMac:textfsm-example claudia$ ls -al
total 24
drwxr-xr-x    4 claudia  staff   136 May 11 05:06 .
drwx------+ 118 claudia  staff  4012 May 11 05:04 ..
-rw-r--r--@   1 claudia  staff   680 May 11 05:06 cisco_ios_show_version.template
-rw-r--r--@   1 claudia  staff  7425 May 11 05:05 lab-swtich-show-cmds.txt

my environment

(cat_scripts) Claudias-iMac:textfsm-example claudia$ pip freeze
et-xmlfile==1.0.1
graphviz==0.8.2
jdcal==1.3
netaddr==0.7.19
openpyxl==2.5.1
**textfsm==0.3.2**
xlrd==1.1.0
(cat_scripts) Claudias-iMac:textfsm-example claudia$

launch the interpreter and import the textfsm module

(cat_scripts) Claudias-iMac:textfsm-example claudia$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import textfsm

open the template file into a file handle I've called "template" and pass that as the argument to textfsm creating a "parsing object" based on that template, the show version template in our case.

>>> template = open('cisco_ios_show_version.template')
>>> results_template = textfsm.TextFSM(template)

Look at some of the methods available in the results_template object

>>> dir(results_template)
['GetValuesByAttrib', 'MAX_NAME_LEN', 'ParseText', 'Reset', '_AppendRecord', '_AssignVar', '_CheckLine', '_CheckRule', '_ClearAllRecord', '_ClearRecord', '_DEFAULT_OPTIONS', '_GetHeader', '_GetValue', '_Operations', '_Parse', '_ParseFSMState', '_ParseFSMVariables', '_ValidateFSM', '_ValidateOptions', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cur_state', '_cur_state_name', '_line_num', '_options_cls', '_result', 'comment_regex', 'header', 'state_list', 'state_name_re', 'states', 'value_map', 'values']
>>> results_template.value_map
{'UPTIME': '(?P<UPTIME>.+)', 'HOSTNAME': '(?P<HOSTNAME>\\S+)', 'RUNNING_IMAGE': '(?P<RUNNING_IMAGE>\\S+)', 'CONFIG_REGISTER': '(?P<CONFIG_REGISTER>\\S+)', 'HARDWARE': '(?P<HARDWARE>\\S+\\d\\S+)', 'VERSION': '(?P<VERSION>.+?)', 'SERIAL': '(?P<SERIAL>\\S+)', 'ROMMON': '(?P<ROMMON>\\S+)'}
>>> results_template.values
[<textfsm.TextFSMValue object at 0x107c0ac90>, <textfsm.TextFSMValue object at 0x107c181d0>, <textfsm.TextFSMValue object at 0x107c18450>, <textfsm.TextFSMValue object at 0x107cf1750>, <textfsm.TextFSMValue object at 0x107cf1790>, <textfsm.TextFSMValue object at 0x107cf17d0>, <textfsm.TextFSMValue object at 0x107cf18d0>, <textfsm.TextFSMValue object at 0x107cf1950>]

you don't need the last two commands. I just wanted to show you how to investigate the object a little. This is the heart of what we are trying to do now that we've selected our parsing template and created an object that will parse our content and pull out the information we want. First we open our text file of show commands. Below I create a file handle called "content2parse" and read the contents of that file into the variable content.

>>> content2parse = open('lab-swtich-show-cmds.txt')
>>> content = content2parse.read()

Now we parse out the data we want using our results_template object (and its ParseText) method against our content and store the results in the parsed_results variable. As you can see this is a list of lists. If you ran this agains show command files from 5 different devices you can begin to see the possibilities. You could have a list of 5 lists with the show version information for each device.

>>> parsed_results = results_template.ParseText(content)
>>> parsed_results
[['15.2(2)E3', 'Bootstrap', 'artic-sw01', '4 days, 14 hours, 2 minutes', 'c2960s-universalk9-mz.152-2.E3.bin', ['WS-C2960S-24TS-S'], ['FOC1709W1DT'], '0xF']]

Your first inclination might be to iterate over the results but remember that its a list of lists so that would iterate over the only item in the "top level" list. What you want is to iterate over each element (which is a list) of the list. I know that my list has 1 element, the [0] element, and that list has 8 elements. If I iterate over the [0] element of the list I get each individual bit of information

>>> len(parsed_results)
1
>>> len(parsed_results[0])
8
>>> for item in parsed_results:
...   print(item)
...
['15.2(2)E3', 'Bootstrap', 'artic-sw01', '4 days, 14 hours, 2 minutes', 'c2960s-universalk9-mz.152-2.E3.bin', ['WS-C2960S-24TS-S'], ['FOC1709W1DT'], '0xF']
>>> for item in parsed_results[0]:
...   print(item)
...
15.2(2)E3
Bootstrap
artic-sw01
4 days, 14 hours, 2 minutes
c2960s-universalk9-mz.152-2.E3.bin
['WS-C2960S-24TS-S']
['FOC1709W1DT']
0xF

I know I have 8 elements because i checked the length and if you do a length on the value map you can see that the template is designed to get 8 pieces of information so that makes sense.

>>> len(parsed_results[0])
8
>>> len(results_template.value_map)
8

Finally, after all of this I wanted to get the version and hardware information out of this file and here it is:

>>> parsed_results[0][0]
'15.2(2)E3'
>>> parsed_results[0][5]
['WS-C2960S-24TS-S']
>>>

I hope these basics help you get started. The real beauty of course is parsing out information real time against your actual devices although working against show command files winds up being very useful as well (for what I do at any rate).

@jmcgill298
Copy link
Contributor

@yusufq8 does this give you the help you need to get started?

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

3 participants