better ethool and device type guessing
This commit is contained in:
parent
4ddbb89c18
commit
15faf59182
3 changed files with 99 additions and 44 deletions
|
@ -1,3 +1,6 @@
|
|||
import re
|
||||
import subprocess
|
||||
|
||||
# Originally from https://github.com/opencoff/useful-scripts/blob/master/linktest.py
|
||||
|
||||
# mapping fields from ethtool output to simple names
|
||||
|
@ -15,28 +18,53 @@ field_map = {
|
|||
}
|
||||
|
||||
|
||||
def parse_ethtool_output(data):
|
||||
class Ethtool():
|
||||
"""
|
||||
parse ethtool output
|
||||
This class aims to parse ethtool output
|
||||
There is several bindings to have something proper, but it requires
|
||||
compilation and other requirements.
|
||||
"""
|
||||
def __init__(self, interface, *args, **kwargs):
|
||||
self.interface = interface
|
||||
|
||||
fields = {}
|
||||
field = ''
|
||||
fields['speed'] = '-'
|
||||
fields['link'] = '-'
|
||||
fields['duplex'] = '-'
|
||||
for line in data.split('\n')[1:]:
|
||||
line = line.rstrip()
|
||||
r = line.find(':')
|
||||
if r > 0:
|
||||
field = line[:r].strip()
|
||||
if field not in field_map:
|
||||
continue
|
||||
field = field_map[field]
|
||||
data = line[r+1:].strip()
|
||||
fields[field] = data
|
||||
else:
|
||||
if len(field) > 0 and \
|
||||
field in field_map.keys():
|
||||
fields[field] += ' ' + line.strip()
|
||||
return fields
|
||||
def _parse_ethtool_output(self):
|
||||
"""
|
||||
parse ethtool output
|
||||
"""
|
||||
|
||||
output = subprocess.getoutput('ethtool {}'.format(self.interface))
|
||||
|
||||
fields = {}
|
||||
field = ''
|
||||
fields['speed'] = '-'
|
||||
fields['link'] = '-'
|
||||
fields['duplex'] = '-'
|
||||
for line in output.split('\n')[1:]:
|
||||
line = line.rstrip()
|
||||
r = line.find(':')
|
||||
if r > 0:
|
||||
field = line[:r].strip()
|
||||
if field not in field_map:
|
||||
continue
|
||||
field = field_map[field]
|
||||
output = line[r+1:].strip()
|
||||
fields[field] = output
|
||||
else:
|
||||
if len(field) > 0 and \
|
||||
field in field_map:
|
||||
fields[field] += ' ' + line.strip()
|
||||
return fields
|
||||
|
||||
def _parse_ethtool_module_output(self):
|
||||
status, output = subprocess.getstatusoutput('ethtool -m {}'.format(self.interface))
|
||||
if status != 0:
|
||||
return {}
|
||||
r = re.search(r'Identifier.*\((\w+)\)', output)
|
||||
if r and r.groups() > 0:
|
||||
return {'form_factor': r.groups()[0]}
|
||||
|
||||
def parse(self):
|
||||
return {
|
||||
**self._parse_ethtool_output(),
|
||||
**self._parse_ethtool_module_output(),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue