0

I am trying to read file (sample below). I need to display 2 different set of data

  1. first data is not in series (i.e not continuous - spread across text file)

The lines i need to display should have string i am looking for in searchstrings

def basic():
    searchstrings = ['Device name:', 'Switch type is', 'Kernel uptime is' , 'NXOS: version' ] 
    for line in fh:
        for word in searchstrings:
            if word in line:
                print line

The issue with my above code is multiple lines are getting displayed as shown below:

NXOS: version 5.2
Device name: N11k
Kernel uptime is 55 day(s), 23 hour(s), 53 minute(s), 11 second(s)
NXOS: version 5.2
Device name: N11k
Kernel uptime is 55 day(s), 23 hour(s), 53 minute(s), 12 second(s)
Switch type is : Nexus1100 (8 Slot) Chassis

I guess my for loop logic is incorrect - need help in fixing it. Also is it possible to save the line in variable which can be used for printing later ?

  1. second data is in series (continuous chunk) I want to read from 'show module' till line above 'show license' I tried suggestion listed here - python - Read file from and to specific lines of text but could not make it to work - i get no output at all

Thanks, Victor

File which is being read:

Software
BIOS: version 06.20
NXOS: version 5.2
BIOS compile time: 11/07/2013
NXOS image file is: bootflash:///n1000-dk9.5.2.bin
NXOS compile time: 6/3/2014 13:00:00 [06/18/2014 23:35:53]

Hardware
cisco Nexus1000 C1508 (8 Slot) Chassis ("Supervisor Module")
Intel(R) Xeon(R) CPU E5-2403 with 16402460 kB of memory.
Processor Board ID SAL

Device name: N1k-LabSystem
bootflash: 21 kB
Kernel uptime is 55 day(s), 23 hour(s), 53 minute(s), 11 second(s)

Last reset
Reason: Unknown
System version: 5.2
Service:

Switch type is : Nexus1100 (8 Slot) Chassis

show module
Mod Ports Module-Type Model Status


2 52 48x1/10G SFP+ 4x40G Ethernet Module N11K-X9564PX ok
3 36 36p 40G Ethernet Module N11k-X9636PQ ok
21 0 Fabric Module N11k-FM ok
22 0 Fabric Module N11k-FM ok
23 0 Fabric Module N11k-FM ok
24 0 Fabric Module N11k-FM ok
25 0 Fabric Module N11k-FM ok
26 0 Fabric Module N11k-FM ok
27 0 Supervisor Module N11k-SUP-A active *
29 0 System Controller N11k-SC-A active
30 0 System Controller N11k-SC-A standby

Mod Sw Hw Slot


2 5.2 1.0 LC2
3 5.2 1.0 LC3
21 5.2 1.1 FM1
22 5.2 1.1 FM2

'show license'
XYZABNCD

Community
  • 1
  • 1
Victor
  • 1

1 Answers1

0

The problem is outside of your basic function. I tried it and lines are not duplicated. It may only come from the way you call it. If you want to keep them to print later, you should write :

def basic(fh): resul = [] searchstrings = ['Device name:', 'Switch type is', 'Kernel uptime is' , 'NXOS: version' ] for line in fh: for word in searchstrings: if word in line: resul.append(line.strip()) break return resul

That way :

  • even if line contained multiple searched words, it would be taken only once
  • you pass fh as a parameter instead of using a global
  • you return the list of matching lines

To get lines between show module and show license, you could use :

def between(fh, first, last)
    ok = False
    for line in fh:
        if last in line: break
        if ok: print line.rstrip()
        if first in line: ok=True

between('show module', 'show license')
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252