117
117
if getattr(test_case, 'release', ''):
118
118
releases.add(getattr(test_case, 'release'))
119
119
return sorted(releases)
122
def _parse_ifconfig_xenial(ifconfig_out):
123
"""Parse ifconfig output from xenial or earlier and return a dictionary.
124
given content like below, return:
125
{'eth0': {'address': '10.8.1.78', 'broadcast': '10.8.1.255',
126
'inet6': [{'address': 'fe80::216:3eff:fe63:c05d',
127
'prefixlen': '64', 'scope': 'Link'},
128
{'address': 'fdec:2922:2f07:0:216:3eff:fe63:c05d',
129
'prefixlen': '64', 'scope': 'Global'}],
130
'interface': 'eth0', 'link_encap': 'Ethernet',
131
'mac_address': '00:16:3e:63:c0:5d', 'mtu': 1500,
132
'multicast': True, 'netmask': '255.255.255.0',
133
'running': True, 'up': True}}
135
eth0 Link encap:Ethernet HWaddr 00:16:3e:63:c0:5d
136
inet addr:10.8.1.78 Bcast:10.8.1.255 Mask:255.255.255.0
137
inet6 addr: fe80::216:3eff:fe63:c05d/64 Scope:Link
138
inet6 addr: fdec:2922:2f07:0:216:3eff:fe63:c05d/64 Scope:Global
139
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
140
RX packets:21503 errors:0 dropped:0 overruns:0 frame:0
141
TX packets:11346 errors:0 dropped:0 overruns:0 carrier:0
142
collisions:0 txqueuelen:1000
143
RX bytes:31556357 (31.5 MB) TX bytes:870943 (870.9 KB)
146
combined_fields = {'addr': 'address', 'Bcast': 'broadcast',
147
'Mask': 'netmask', 'MTU': 'mtu',
148
'encap': 'link_encap'}
149
boolmap = {'RUNNING': 'running', 'UP': 'up', 'MULTICAST': 'multicast'}
151
for line in ifconfig_out.splitlines():
154
if not line.startswith(" "):
155
cur_iface = line.split()[0].rstrip(":")
156
cur_data = {'inet6': [], 'interface': cur_iface}
157
for t in boolmap.values():
159
ifaces[cur_iface] = cur_data
163
if toks[0] == "inet6":
165
address, prefixlen = cidr.split("/")
166
scope = toks[3].split(":")[1]
167
cur_ipv6 = {'address': address, 'scope': scope,
168
'prefixlen': prefixlen}
169
cur_data['inet6'].append(cur_ipv6)
172
for i in range(0, len(toks)):
179
if cur_tok == "HWaddr":
180
cur_data['mac_address'] = next_tok
182
key, _colon, val = cur_tok.partition(":")
183
if key in combined_fields:
184
cur_data[combined_fields[key]] = val
185
elif cur_tok in boolmap:
186
cur_data[boolmap[cur_tok]] = True
188
if 'mtu' in cur_data:
189
cur_data['mtu'] = int(cur_data['mtu'])
194
def _parse_ifconfig_yakkety(ifconfig_out):
195
"""Parse ifconfig output from yakkety or later(?) and return a dictionary.
197
given ifconfig output like below, return:
198
{'ens2': {'address': '10.5.0.78',
199
'broadcast': '10.5.255.255',
200
'broadcast_flag': True,
201
'inet6': [{'address': 'fe80::f816:3eff:fe05:9673',
202
'prefixlen': '64', 'scopeid': '0x20<link>'},
203
{'address': 'fe80::f816:3eff:fe05:9673',
204
'prefixlen': '64', 'scopeid': '0x20<link>'}],
205
'interface': 'ens2', 'link_encap': 'Ethernet',
206
'mac_address': 'fa:16:3e:05:96:73', 'mtu': 1500,
207
'multicast': True, 'netmask': '255.255.0.0',
208
'running': True, 'up': True}}
210
ens2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
211
inet 10.5.0.78 netmask 255.255.0.0 broadcast 10.5.255.255
212
inet6 fe80::f816:3eff:fe05:9673 prefixlen 64 scopeid 0x20<link>
213
inet6 fe80::f816:3eff:fe05:9673 prefixlen 64 scopeid 0x20<link>
214
ether fa:16:3e:05:96:73 txqueuelen 1000 (Ethernet)
215
RX packets 33196 bytes 48916947 (48.9 MB)
216
RX errors 0 dropped 0 overruns 0 frame 0
217
TX packets 5458 bytes 411486 (411.4 KB)
218
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
220
fmap = {'mtu': 'mtu', 'inet': 'address',
221
'netmask': 'netmask', 'broadcast': 'broadcast',
222
'ether': 'mac_address'}
223
boolmap = {'RUNNING': 'running', 'UP': 'up', 'MULTICAST': 'multicast',
224
'BROADCAST': 'broadcast_flag'}
227
for line in ifconfig_out.splitlines():
230
if not line.startswith(" "):
231
cur_iface = line.split()[0].rstrip(":")
232
cur_data = {'inet6': [], 'interface': cur_iface}
233
for t in boolmap.values():
235
ifaces[cur_iface] = cur_data
238
if toks[0] == "inet6":
239
cur_ipv6 = {'address': toks[1]}
240
cur_data['inet6'].append(cur_ipv6)
242
for i in range(0, len(toks)):
249
cur_data[fmap[cur_tok]] = next_tok
250
elif cur_tok in ('prefixlen', 'scopeid'):
251
cur_ipv6[cur_tok] = next_tok
252
cur_data['inet6'].append
253
elif cur_tok.startswith("flags="):
254
# flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
255
flags = cur_tok[cur_tok.find("<") + 1:
256
cur_tok.rfind(">")].split(",")
259
cur_data[boolmap[flag]] = True
260
elif cur_tok == "(Ethernet)":
261
cur_data['link_encap'] = 'Ethernet'
263
if 'mtu' in cur_data:
264
cur_data['mtu'] = int(cur_data['mtu'])
269
def ifconfig_to_dict(ifconfig_a):
270
# if the first token of the first line ends in a ':' then assume yakkety
271
# parse ifconfig output and return a dictionary.
273
# return a dictionary of network information like:
274
# {'ens2': {'address': '10.5.0.78', 'broadcast': '10.5.255.255',
275
# 'broadcast_flag': True,
276
# 'inet6': [{'address': 'fe80::f816:3eff:fe05:9673',
277
# 'prefixlen': '64', 'scopeid': '0x20<link>'},
278
# {'address': 'fe80::f816:3eff:fe05:9673',
279
# 'prefixlen': '64', 'scopeid': '0x20<link>'}],
280
# 'interface': 'ens2', 'link_encap': 'Ethernet',
281
# 'mac_address': 'fa:16:3e:05:96:73', 'mtu': 1500,
282
# 'multicast': True, 'netmask': '255.255.0.0',
283
# 'running': True, 'up': True}}
284
line = ifconfig_a.lstrip().splitlines()[0]
285
if line.split()[0].endswith(":"):
286
return _parse_ifconfig_yakkety(ifconfig_a)
288
return _parse_ifconfig_xenial(ifconfig_a)