您好我已经在我们所有的服务器中收集了我们的nic绑定的当前配置并将其放在一个文件中。以下数据示例:
文件名:temp2
Server1
Currently Active Slave: eth0
Slave Interface: eth0
Slave Interface: eth1
Server2
Currently Active Slave: eth1
Slave Interface: eth0
Slave Interface: eth1
ServerN
....
....
抱歉,我很少使用python,所以我认为我自己是一个新手,我想写一个python脚本,将读取文件,并检查每个服务器打印只有不活动的界面。有什么建议吗?到目前为止,我已经开始它只是读取文件,卡在如何只打印非当前活动的接口。
#!/usr/bin/python3.4
content = open('/home/Workspace/temp2', 'r')
print content.read()
# Compare the Active from the two interface;then
print Not Active
# >> Example: Server1
# >> Current Active: eth0
print eth1
content.close()
答案 0 :(得分:0)
这将为您解析文件:
serv, intf = None, None
for line in open('/home/Workspace/temp2'):
if line.startswith('Server'):
serv = line.strip()
elif line.startswith('Currently Active Slave: '):
intf = line.split(':')[-1].strip()
if serv is not None and intf is not None:
print('{} - {}'.format(serv, intf))
serv, intf = None, None
答案 1 :(得分:0)
我们需要一个服务器名称列表,以便我们可以跟踪我们当前正在阅读的服务器。
如果该行是服务器,那么我们查看哪个接口处于活动状态。我们解析每一行,如果接口处于非活动状态,我们将其附加到列表中。
names = ['Server1', 'Server2', 'Server3']
inactives = {}
for name in names:
inactives[name] = []
currentServer = None
for line in open('/home/Workspace/temp2'):
if line.strip() in names:
currentServer = line.strip()
active = None
if line.startswith('Currently Active Slave: '):
active = line.split(':')[-1].strip()
if active and line.startswith('Slave Interface: '):
interface = line.split(':')[-1].strip()
if interface != active:
inactives[currentServer].append(interface)
print(inactives)