change if names from enp*s0 to eth*
including all cfg files!
This commit is contained in:
24
utils.py
24
utils.py
@ -6,14 +6,6 @@ from configparser import ConfigParser
|
||||
from netifaces import interfaces, ifaddresses, gateways, AF_INET, AF_LINK
|
||||
|
||||
|
||||
ifname_mapping = {
|
||||
'eth0': 'enp1s0',
|
||||
'eth1': 'enp2s0',
|
||||
'eth2': 'enp3s0',
|
||||
'eth3': 'enp4s0',
|
||||
}
|
||||
|
||||
|
||||
if os.geteuid():
|
||||
def sudo(cmd):
|
||||
os.system(f'sudo {cmd}')
|
||||
@ -30,6 +22,19 @@ def get_config(section=None):
|
||||
"""
|
||||
parser = ConfigParser()
|
||||
cfgfiles = glob('/home/l_samenv/boxtools/cfg/%s_*.cfg' % socket.gethostname())
|
||||
if not cfgfiles:
|
||||
# look by id
|
||||
boxid = None
|
||||
for ifname in ('enp1s0', 'eth0'):
|
||||
try:
|
||||
with open(f'/sys/class/net/{ifname}/address') as f:
|
||||
addr = f.read().strip().lower()
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
boxid = int(''.join(addr.split(':')[-3:]), 16) & 0xffffff
|
||||
break
|
||||
if boxid:
|
||||
cfgfiles = glob('/home/l_samenv/boxtools/cfg/*_%6.6x.cfg' % boxid)
|
||||
if len(cfgfiles) != 1:
|
||||
raise ValueError('there must be one and only one single cfgfile %r' % cfgfiles)
|
||||
parser.read(cfgfiles[0])
|
||||
@ -37,9 +42,6 @@ def get_config(section=None):
|
||||
result = {k: dict(parser[k]) for k in ([section] if section else parser.sections())}
|
||||
except KeyError:
|
||||
return {}
|
||||
network = result.get('NETWORK', {})
|
||||
for name in list(network):
|
||||
network[ifname_mapping.get(name, name)] = network.pop(name)
|
||||
if section:
|
||||
return result[section]
|
||||
return result
|
||||
|
Reference in New Issue
Block a user