<pre><code>with open('interfaces.txt') as infile:
# Each interface must be separated by a blank line as in the sample
interfaces = infile.read().split('\n\n')
interfaces = map(str.splitlines, interfaces)
interfaces = {lines[0][:-1]: [line.strip() for line in lines[1:]]
for lines in interfaces}
def interface_value(interface_name, line_prefix):
# Make this argument case-insensitive to make the function easier to use
line_prefix = line_prefix.lower()
lines = [line for line in interfaces[interface_name]
if line.lower().startswith(line_prefix)]
if len(lines) == 0:
raise ValueError('No lines found with given prefix')
if len(lines) > 1:
raise ValueError('Multiple lines found with given prefix: \n %s' % lines)
return lines[0][len(line_prefix):].strip(': ')
eth1_flags = interface_value('eth1', 'flags')
eth2_mtu = interface_value('eth2', 'Maximum Transfer Unit size is')
print(eth1_flags)
print(eth2_mtu)
</code></pre>
<p>输出:</p>
<pre><code>(0x1043) UP BROADCAST MULTICAST TRAILERS ARP RUNNING
1500
</code></pre>
<p>我将让您执行额外的解析,比如从<code>"10.196.135.30 Vlan: 0"</code>中获取IP。你知道吗</p>