3

If i have a text file like this:

FastEthernet3/1
ip address 0.0.0.0
enable portfast

FastEthernet3/2
ip address 0.0.0.0
enable portfast

FastEthernet3/3
ip address 0.0.0.0

FastEthernet3/4
ip address 0.0.0.0

And i would like to print out the interfaces that has not enabled portfast. How do i print this in python?

i have the following code:

import os
import sys

root = "path to text file like the example above"

os.chdir(root)

current2 = os.getcwd()

print ("CWD = ", current2,"\n")


file = sys.argv[1]

f = open(file)
contents = f.read()
f.close()
print ("Number of GigabitEthernet:",contents.count("interface GigabitEthernet"))
print ("Number of FastEthernet:",contents.count("FastEthernet"))


x = open(file)
string1 = "enable portfast"
for line in x.readlines():
    if line.startswith(string1)
        print (line)
filehandle.close()

so i can find the line with enable portfast and print it, but i want it to print more lines so i know witch interface has portfast enabled.

5
  • store the port number and the enabled status till you reach the next block or end of file and then you know what to print Sep 20, 2012 at 11:13
  • What's the deal with line.endswith(string2)?
    – Alexander
    Sep 20, 2012 at 11:16
  • 1
    I like l4mpi's solution, but are there interfaces with "interface GigabitEthernet" ?
    – Minion91
    Sep 20, 2012 at 11:25
  • yes but this is just an example text file, i use an cisco router config file
    – DT22
    Sep 20, 2012 at 11:31
  • Are they all delimited by whitelines ?
    – Minion91
    Sep 20, 2012 at 11:34

6 Answers 6

2

Splitting by interfaces based in the blank line separation:

import re
pinterfaces = re.compile("\r?\n\r?\n").split(contents)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print pinterface

FastEthernet3/1
ip address 0.0.0.0
enable portfast
FastEthernet3/2
ip address 0.0.0.0
enable portfast
1
  • No need for re, you could just use contents.split("\n\n").
    – l4mpi
    Sep 20, 2012 at 11:34
2

There is no way to go back into the file so the best solution would be to keep track of the previous lines and print them when the portFast matches.

Edit to include solution: (Credit to Alexander)

import re
pinterfaces = re.compile("\r?\n\r?\n").split(contents)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print pinterface
2
  • @ Minion91 -- wheres the diffeREnce? :)
    – root
    Sep 20, 2012 at 11:49
  • @root I like re's they make me smile :)
    – Minion91
    Sep 20, 2012 at 11:51
2

If every interface definition starts with the string "FastEthernet", you could just split your contents by that string:

interfaces = contents.split("FastEthernet"):
for interface in interfaces:
    if "enable portfast" in interface:
        print "FastEthernet" + interface

Edit: Based on Alexanders solution, if there is always a blank line separating the interfaces, just declare interfaces like this:

interfaces = contents.split("\n\n")

... and change the print statement to print interface only.

1
  • 1
    Don't forget to remove the "FastEthernet" if you split on blank lines
    – Minion91
    Sep 20, 2012 at 11:42
0
with open('test.txt', 'r') as in_file:
    lines = in_file.readlines()

for i,v in enumerate(lines):
    #print i,v
    if v.strip() == 'enable portfast':
        print lines[i-2].strip()
        print lines[i-1].strip()
        print lines[i].strip()

This prints out:

FastEthernet3/1
ip address 0.0.0.0
enable portfast
FastEthernet3/2
ip address 0.0.0.0
enable portfast
1
  • This assumes that every matching interface definition has exactly three lines.
    – l4mpi
    Sep 20, 2012 at 11:32
0

Thnx to Alexander it works now :) the only thing is my text file was a little different then the example. It looked like this:

FastEthernet3/1
ip address 0.0.0.0
enable portfast
!
FastEthernet3/2
ip address 0.0.0.0
enable portfast
!
FastEthernet3/3
ip address 0.0.0.0
!
FastEthernet3/4
ip address 0.0.0.0

So i had te replace the ! with a white space first and then it worked :)

import re
contents2 = contents.replace("!","")
pinterfaces = re.compile("\n\n").split(contents2)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print (pinterface)
-1

Try parsing all devices separately in to an array of dictionaries, like this:

[{"device_name":"FastEthernet/1","address":"1.0.0.0", "portfast": True}]

then you can loop over this hash and print the device name of the items that have the portfast value.

3
  • 4
    I don't think that is what he wants
    – Minion91
    Sep 20, 2012 at 11:15
  • Sorry I corrected it after I read it trough this might be closer. Sep 20, 2012 at 11:19
  • Well the good solution should be implementing a class that can read all of the lines and parse them in to a hash.. then you can loop over that hash and print the device if it is using enable portfast. Sep 20, 2012 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.