Skip to content

Instantly share code, notes, and snippets.

@carlpett
Created February 17, 2016 08:26
Show Gist options
  • Save carlpett/cf21fce62325cdd20901 to your computer and use it in GitHub Desktop.
Save carlpett/cf21fce62325cdd20901 to your computer and use it in GitHub Desktop.
import logging
import subprocess
import salt.utils
log = logging.getLogger(__name__)
def primary_interface():
try:
import salt.utils.network as network
except Exception as e:
log.warn('Could not import salt.utils.network: %s' % e)
return {}
grains = {}
interfaceLabel = _get_default_interface()
interface = network.interface(interfaceLabel)
grains['primary_interface'] = interface
return grains
def _get_default_interface():
if salt.utils.is_windows():
return _windows_get_default_interface()
else:
return _linux_get_default_interface()
def _windows_get_default_interface():
powershellScript = ['$interfaceIndices = (Get-NetIPConfiguration | Foreach IPv4DefaultGateway).ifIndex',
'$primaryInterface = $interfaceIndices | Foreach-Object { Get-NetIPInterface -AddressFamily IPv4 -InterfaceIndex $_ | Sort-Object InterfaceMetric | Select -First 1 }',
'$primaryInterface | Get-NetAdapter | Select -ExpandProperty InterfaceDescription']
return subprocess.check_output(['powershell', '-NoProfile', '-Command', ';'.join(powershellScript)]).strip()
def _linux_get_default_interface():
import shlex
cmd = shlex.split('ip -4 route list 0/0')
return subprocess.check_output(cmd).split()[4]
@mo-mughrabi
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment