417
417
return(''.join(random_chars))
420
def list_nics(nic_type):
420
def is_phy_iface(interface):
421
"""Returns True if interface is not virtual, otherwise False."""
423
sys_net = '/sys/class/net'
424
if os.path.isdir(sys_net):
425
for iface in glob.glob(os.path.join(sys_net, '*')):
426
if '/virtual/' in os.path.realpath(iface):
429
if interface == os.path.basename(iface):
435
def get_bond_master(interface):
436
"""Returns bond master if interface is bond slave otherwise None.
438
NOTE: the provided interface is expected to be physical
441
iface_path = '/sys/class/net/%s' % (interface)
442
if os.path.exists(iface_path):
443
if '/virtual/' in os.path.realpath(iface_path):
446
master = os.path.join(iface_path, 'master')
447
if os.path.exists(master):
448
master = os.path.realpath(master)
449
# make sure it is a bond master
450
if os.path.exists(os.path.join(master, 'bonding')):
451
return os.path.basename(master)
456
def list_nics(nic_type=None):
421
457
'''Return a list of nics of given type(s)'''
422
458
if isinstance(nic_type, six.string_types):
423
459
int_types = [nic_type]
425
461
int_types = nic_type
427
for int_type in int_types:
428
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
465
for int_type in int_types:
466
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
467
ip_output = subprocess.check_output(cmd).decode('UTF-8')
468
ip_output = ip_output.split('\n')
469
ip_output = (line for line in ip_output if line)
470
for line in ip_output:
471
if line.split()[1].startswith(int_type):
472
matched = re.search('.*: (' + int_type +
473
r'[0-9]+\.[0-9]+)@.*', line)
475
iface = matched.groups()[0]
477
iface = line.split()[1].replace(":", "")
479
if iface not in interfaces:
480
interfaces.append(iface)
429
483
ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
430
ip_output = (line for line in ip_output if line)
484
ip_output = (line.strip() for line in ip_output if line)
486
key = re.compile('^[0-9]+:\s+(.+):')
431
487
for line in ip_output:
432
if line.split()[1].startswith(int_type):
433
matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line)
435
interface = matched.groups()[0]
437
interface = line.split()[1].replace(":", "")
438
interfaces.append(interface)
488
matched = re.search(key, line)
490
iface = matched.group(1)
491
iface = iface.partition("@")[0]
492
if iface not in interfaces:
493
interfaces.append(iface)
440
495
return interfaces