我是python的新手,我需要阅读下面的文件(实际上下面的输出从命令中获取)并打印其输出。
以下是文件:
NIC:
Name: eth0
ID: 0 1
Dynamic MAC Addr: 00:00:00:00:00:01
Preference: NONE
Ethernet Interface:
Name: prod
Default Network: Yes
VLAN ID: 2
Operational VLAN: SWA
Config Qual: N/A
Name: dev
Default Network: No
VLAN ID: 3
Operational VLAN: SWA
Config Qual: N/A
Name: eth1
Fabric ID: 1 0
Dynamic MAC Addr: 00:00:00:00:00:11
Preference: NONE
Ethernet Interface:
Name: prod
Default Network: Yes
VLAN ID: 2
Operational VLAN: SWB
Config Qual: N/A
Name: dev
Default Network: No
VLAN ID: 3
Operational VLAN: SWB
Config Qual: N/A
实际上我要实现的是我需要找到多少接口。在上述情况下,eth0和eth1。
我想验证eth0有ID 0 1
对于eth0,我需要验证是否存在Name prod,vlan ID 2是否为prod而默认网络是否为
对于eth0,我需要验证Name dev是否存在,vlan ID 3是否为dev,默认网络为no。
同样适用于所有界面。通常我会得到eth0 - 20的输出。
到目前为止,我尝试使用以下方法并且能够使用以下方法使其工作但是代码不正确,因为如果字段互换,它将无法工作。
我实际上在做的是找到eth0并将值重定向到var 1,var2,var3等等,然后使用这些var1,var2,var3来获取输出。
但是当字段互换以及将额外字段添加到输出文件时,这不会起作用。
(注意以下代码是我正在尝试的例子)
我需要你的帮助才能以正确的方式实现这一目标。
from itertools import islice
import string
import re
import os
import sys
a = "eth0"
def silentremove(filename): # remove file
try:
os.remove(filename)
except OSError as e: # this would be "except OSError, e:"
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occurred
file = "inputfile.txt"
silentremove(file)
with open("vnic1.txt") as f: # Remove empty lines
for line in f:
if not line.isspace():
input_file = open('inputfile.txt', 'a')
sys.stdout = input_file
sys.stdout.write(line)
input_file.close()
sys.stdout = sys.__stdout__
d = {}
count = 0
with open('inputfile.txt') as f:
for line in f:
if (re.search(a, line, re.IGNORECASE)):
var1, var2, var3, var4, var5,var6,var7,var8,var9,var10,var11,var12,var13, var14, var15, var16 = islice(f, 16)
key, value = var1.strip().split(":", 1)
d[key] = value
ideth0 = "0 1"
if (re.search(ideth0, value, re.IGNORECASE)):
count +=1
key, value = var5.strip().split(":", 1)
d[key] = value
name = "prod"
if (re.search(name, value, re.IGNORECASE)):
count +=1
if count == 2:
print "Working"
print "Status-{}:".format(a)
print " ID Name"
print " {} {}".format(ideth0, name)
else:
print "Failed"
答案 0 :(得分:1)
该文字看起来像是YAML,但它不是,但它可能是
with open("vnic1.txt") as f:
data = f.read()
# makeing it yaml-string
data = dat.replace(" Name","- Name") # the 2 spaces are intended
import yaml
data = yaml.load(data) # you now have the entire structure as a huge dict, profit
这样你就可以拥有类似这样的数据
{
'NIC': [
{'Dynamic MAC Addr': '00:00:00:00:00:01',
'Ethernet Interface': [{'Config Qual': 'N/A',
'Default Network': True,
'Name': 'prod',
'Operational VLAN': 'SWA',
'VLAN ID': 2},
{'Config Qual': 'N/A',
'Default Network': False,
'Name': 'dev',
'Operational VLAN': 'SWA',
'VLAN ID': 3}],
'ID': '0 1',
'Name': 'eth0',
'Preference': 'NONE'},
{'Dynamic MAC Addr': '00:00:00:00:00:11',
'Ethernet Interface': [{'Config Qual': 'N/A',
'Default Network': True,
'Name': 'prod',
'Operational VLAN': 'SWB',
'VLAN ID': 2},
{'Config Qual': 'N/A',
'Default Network': False,
'Name': 'dev',
'Operational VLAN': 'SWB',
'VLAN ID': 3}],
'Fabric ID': '1 0',
'Name': 'eth1',
'Preference': 'NONE'}
]
}
这可能不是你要求的,但肯定会更容易导航(至少你有正确分开的部分)