【发布时间】:2012-09-20 11:10:35
【问题描述】:
如果我有这样的文本文件:
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
我想打印出没有启用 portfast 的接口。我如何在 python 中打印这个?
我有以下代码:
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()
所以我可以找到启用 portfast 的行并打印它,但我希望它打印更多行,所以我知道女巫接口启用了 portfast。
【问题讨论】:
-
存储端口号和启用状态,直到您到达下一个块或文件末尾,然后您就知道要打印什么了
-
line.endswith(string2)是怎么回事? -
我喜欢l4mpi的方案,但是有
"interface GigabitEthernet"的接口吗? -
是的,但这只是一个示例文本文件,我使用的是 cisco 路由器配置文件
-
都是白线分隔的吗?