Last active
December 23, 2015 00:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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