Question

Not sure if the title of this question is correct or not, so let me give a bit of background information. I have two text files. One called data.txt and the other called results.txt. In the data file I have the result of a "show version" on a Cisco networking device. It looks like the following:

Cisco IOS Software, s72033_rp Software (s72033_rp-ADVIPSERVICESK9_WAN-M), Version 12.2(33)SXI4, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2010 by Cisco Systems, Inc.
Compiled Sat 29-May-10 17:54 by prod_rel_team

ROM: System Bootstrap, Version 12.2(17r)SX6, RELEASE SOFTWARE (fc1)

 core-router uptime is 2 years, 5 weeks, 1 day, 5 hours, 47 minutes
Uptime for this control processor is 2 years, 5 weeks, 1 day, 4 hours, 50 minutes
Time since san-qrc1 switched to active is 2 years, 5 weeks, 1 day, 4 hours, 56 minutes
System returned to ROM by reload at 16:12:08 PDT Fri Aug 27 2010 (SP by reload)
System restarted at 16:19:33 PDT Fri Aug 27 2010
System image file is "sup-bootdisk:s72033-advipservicesk9_wan-mz.122-33.SXI4.bin"
Last reload reason: Reload Command



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-C6509-E (R7000) processor (revision 1.5) with 983008K/65536K bytes of memory.
Processor board ID XXXXXXXXXX
SR71000 CPU at 600Mhz, Implementation 0x504, Rev 1.2, 512KB L2 Cache
Last reset from s/w reset
35 Virtual Ethernet interfaces
51 Gigabit Ethernet interfaces
26 Ten Gigabit Ethernet interfaces
1917K bytes of non-volatile configuration memory.
8192K bytes of packet buffer memory.

65536K bytes of Flash internal SIMM (Sector size 512K).
Configuration register is 0x2102

Simply put, I want to read the data.txt and pull out certain strings and put them in the results.txt. A CSV format would be nice, but I'd be happy with just getting the data extracted.

For instance, the script would pull out pertinent data like the hostname of the device (in this case, core-router), System image file name ( s72033-advipservicesk9_wan-mz.122-33.SXI4.bin), uptime (2 years, 5 weeks, 1 day, 5 hours, 47 minutes), serial number (XXXXXXXX), & model (WS-C6509-E). All that information would be put in a tab delimited format into results.txt.

In the future, different data.txt files could be utilized, and the data appended to the results.txt, giving me a running tally of data points. I'm hoping that makes sense. I've tried to do searches in regards to what I'm looking for, but the most of the things I found either talk about finding a whole line in a text file, or getting the index number of the occurrence of a word.

One last thing: depending on the model of Cisco device, all items will be different. The words around it will generally be the same, but the items I'm looking for will be different. Any assistance you can provide would be greatly appreciated. Thanks in advance.

UPDATE

I utilized the script you provided, but I still see the following:

python
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("data.txt") as infile:
...     text = infile.read()
... 
>>> import re
>>> regex = re.compile(
...     r"""^(?P<device>\S*)           # Match non-whitespace device name
...     \suptime\sis\s                 # Match " uptime is "
...     (?P<uptime>[^\r\n]*)           # Match until end of line --> uptime
...     .*?^System\simage\sfile\sis\s  # Match intervening text
...     "[^:]*:                        # Match from quote to colon
...     (?P<sifilename>[^"]*)          # Match everything until quote --> filename
...     .*?^cisco\s                    # Match intervening text
...     (?P<model>\S*)                 # Match non-whitespace model name
...     .*?^Processor\sboard\sID\s     # Match intervening text
...     (?P<serialno>[^\r\n]*)         # Match until end of line --> serial no""", 
...     re.DOTALL | re.MULTILINE | re.VERBOSE)
>>> match = regex.search(text)
>>> match.groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'

The issue is the hostname is indented by 1 space. When I pasted it, the indention disappeared. However, when the command "show version" is issued, that 1 space indent appears. Trying to run the code on the above will break the script. Removing the space allows it to work.

Was it helpful?

Solution

You could read the file into a string like this:

with open("data.txt") as infile:
    text = infile.read()

Then you could use a regex to extract the relevant info:

import re
regex = re.compile(
    r"""^(?P<device>\S*)           # Match non-whitespace device name
    \suptime\sis\s                 # Match " uptime is "
    (?P<uptime>[^\r\n]*)           # Match until end of line --> uptime
    .*?^System\simage\sfile\sis\s  # Match intervening text
    "[^:]*:                        # Match from quote to colon
    (?P<sifilename>[^"]*)          # Match everything until quote --> filename
    .*?^cisco\s                    # Match intervening text
    (?P<model>\S*)                 # Match non-whitespace model name
    .*?^Processor\sboard\sID\s     # Match intervening text
    (?P<serialno>[^\r\n]*)         # Match until end of line --> serial no""", 
    re.DOTALL | re.MULTILINE | re.VERBOSE)
match = regex.search(text)

Now match.groups() contains:

>>> match.groups()
('core-router', '2 years, 5 weeks, 1 day, 5 hours, 47 minutes', 
's72033-advipservicesk9_wan-mz.122-33.SXI4.bin', 'WS-C6509-E', 'XXXXXXXXXX')

You can use this to write to a csv file like so:

import csv
with open("results.txt", "a") as outfile:
    outcsv = csv.Writer(outfile)
    outcsv.writerow(match.groups())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top