Skip to content

Instantly share code, notes, and snippets.

@Xiol
Created October 8, 2015 11:18
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 Xiol/f8a27b24af936293c862 to your computer and use it in GitHub Desktop.
Save Xiol/f8a27b24af936293c862 to your computer and use it in GitHub Desktop.
Salt grain for discovering primary IP address on a Linux machine.
#!/usr/bin/env python
import subprocess
def linux_get_primary_ip4():
grains = {}
try:
defroute_device = subprocess.check_output("ip route | grep default | grep -Eo 'dev\s+\w+' | awk '{print $2}'", shell=True).strip()
devip = subprocess.check_output("ip a s {0} | grep -E 'inet\s' | cut -d'/' -f1 | awk '{{print $2}}'".format(defroute_device), shell=True).strip()
except subprocess.CalledProcessError as e:
return
if devip is not None or not devip == "":
grains['primary_ip4'] = devip
return grains
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment