我正在编写一段python代码,用来自网络设备的各种show命令替换用于搜索文件的旧php代码。我将适当的值写入csv文件以生成类似cmdb的工作表。 我现在正试图通过文件搜索找到一个特定的文本块,并将其写入另一个临时文件。下面是一段代码:
for root, dirs, _ in os.walk(cmdbDir):
for d in dirs:
dir = os.path.join(root, d)
# Loop through all subdirectories
with os.scandir(dir) as it:
for entry in it:
if entry.name.endswith(".txt") and entry.is_file():
if dir.endswith('juniper'):
manufacturer = 'Juniper'
flag = False
flag2 = False
with open(entry.path, 'r') as f:
for line in f:
# Write a temp file per Juniper Node
nodes = ['node0','node1']
for node in nodes:
tmpfile = (f'{tmpDir}\\{hostname}-{node}.txt')
temp_file = open(tmpfile, 'a')
if line.startswith(f'set groups {node}'):
temp_file.write(line)
# Search for section show chassis hardware
if line.startswith('show chassis hardware'):
flag = True
elif line.startswith('{primary:node0}'):
flag = False
elif flag:
if line.startswith(node):
flag2 = True
elif line.startswith('\n') or len(line) == 0:
flag2 = False
elif flag2:
print(line)
temp_file.write(line)
temp_file.close()
因此,它在目录中循环,并在所有子目录中查找扩展名为.txt的文件。对于这个特殊的Juniper设备,我想对node0和node1的数据进行grep,并将它们放在单独的文件中。当我只为1个节点运行这段代码时(因此我只将node0或node1作为参数传递),它工作得很好,因此grep文本的代码似乎还可以。当我尝试运行node0和node1的代码时,我认为设置标志有问题,然后我在两个文本文件中看到bot节点的输出
这是我循环浏览的原始文件的一部分:
show chassis hardware
node0:
--------------------------------------------------------------------------
Hardware inventory:
Item Version Part number Serial number Description
Chassis CV3016AF1127 SRX300
Routing Engine REV 0x08 650-065039 CV3016AF1127 RE-SRX300
FPC 0 FPC
PIC 0 6xGE,2xGE SFP Base PIC
Power Supply 0
node1:
--------------------------------------------------------------------------
Hardware inventory:
Item Version Part number Serial number Description
Chassis CV3016AF1125 SRX300
Routing Engine REV 0x08 650-065039 CV3016AF1125 RE-SRX300
FPC 0 FPC
PIC 0 6xGE,2xGE SFP Base PIC
Power Supply 0
{primary:node0}
因此,它首先查找“show chassis hardware”和{primary:node0}之间的文本,然后查找“node0”和空行之间的文本,node1的情况大致相同
这是仅使用node0运行此代码时的结果:
set groups node0 system host-name xxx
set groups node0 interfaces fxp0 unit 0 family inet address x.x.x.x/26
set groups node0 interfaces fxp0 unit 0 family inet address x.x.x.x/26 master-only
--------------------------------------------------------------------------
Hardware inventory:
Item Version Part number Serial number Description
Chassis CV3016AF1127 SRX300
Routing Engine REV 0x08 650-065039 CV3016AF1127 RE-SRX300
FPC 0 FPC
PIC 0 6xGE,2xGE SFP Base PIC
Power Supply 0
当我使用node0和node1运行它时:
set groups node1 system host-name xxx
set groups node1 interfaces fxp0 unit 0 family inet address x.x.x.x/26
set groups node1 interfaces fxp0 unit 0 family inet address x.x.x.x/26 master-only
node0:
--------------------------------------------------------------------------
Hardware inventory:
Item Version Part number Serial number Description
Chassis CV3016AF1127 SRX300
Routing Engine REV 0x08 650-065039 CV3016AF1127 RE-SRX300
FPC 0 FPC
PIC 0 6xGE,2xGE SFP Base PIC
Power Supply 0
--------------------------------------------------------------------------
Hardware inventory:
Item Version Part number Serial number Description
Chassis CV3016AF1125 SRX300
Routing Engine REV 0x08 650-065039 CV3016AF1125 RE-SRX300
FPC 0 FPC
PIC 0 6xGE,2xGE SFP Base PIC
Power Supply 0
我试图了解它哪里出了问题,我认为循环中出现了一些问题,某个var没有被取消设置或保持“活动”
我还尝试将这段代码放入函数中,但结果相同
提前谢谢
flag2
必须分别为每个节点设置。为此,将行nodes = ['node0','node1']
向上移动到初始flag2 = False
上方。然后用flag2 = dict.fromkeys(nodes, False)
替换后一行,以后每次使用flag2
替换为flag2[node]
然后代码的相关部分看起来像(未测试):
相关问题 更多 >
编程相关推荐