Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Last active December 23, 2015 00:09
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 cloudnull/6551594 to your computer and use it in GitHub Desktop.
Save cloudnull/6551594 to your computer and use it in GitHub Desktop.
Get CIDR from network interfaces in Ohai.Loads ohai data in as JSON and returns the CIDR based on a device.
import json
import subprocess
# Get Ohai Data
_ohai = subprocess.Popen(['ohai', '-l', 'fatal'], stdout=subprocess.PIPE)
ohai = _ohai.communicate()[0]
data = json.loads(ohai)
# Parse Ohai Data
def get_network(interface):
device = data['network']['interfaces'].get(interface)
if device is not None:
if device.get('routes'):
routes = device['routes']
for net in routes:
if 'scope' in net:
return net.get('destination', '127.0.0.0/8')
break
else:
return '127.0.0.0/8'
else:
return '127.0.0.0/8'
# Get the Subnet from the network interface
network = get_network(interface='eth2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment