Skip to content

Instantly share code, notes, and snippets.

@viq
Created December 30, 2017 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viq/9940f94ae4e99945113d6b8a683c0d17 to your computer and use it in GitHub Desktop.
Save viq/9940f94ae4e99945113d6b8a683c0d17 to your computer and use it in GitHub Desktop.
saltstack custom grain displaying separately physical and virtual network interfaces
# -*- coding: utf-8 -*-
'''
Display virtual and physical network devices separately
'''
from __future__ import absolute_import
# Import python libs
import os
import logging
from os.path import join
# Import salt libs
import salt.utils
log = logging.getLogger(__name__)
def netdevs():
'''
Return list of network devices
'''
if salt.utils.is_linux():
return _linux_netdevs()
else:
log.trace('netdevs grain does not support OS')
def _linux_netdevs():
'''
List physical network devices
'''
ret = {'physical': [], 'virtual': []}
for dev in os.listdir('/sys/devices/'):
if 'pci' in dev:
for root, dirs, files in os.walk(join('/sys/devices/', dev)):
if 'net' in dirs:
# print(os.listdir(join(root, 'net')))
physdev = os.listdir(join(root, 'net'))[0]
ret['physical'].append(physdev)
log.trace('Device {0} reports itself as a physical network interface'.format(physdev))
for dev in os.listdir('/sys/devices/virtual/net'):
ret['virtual'].append(dev)
log.trace('Device {0} reports itself as a virtual network interface'.format(dev))
return {'interfaces': ret}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment