Created
March 3, 2016 01:22
-
-
Save anonymous/4bfc791bc876d71d34a5 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import salt.config | |
import salt.loader | |
from socket import AF_INET, inet_pton | |
from struct import unpack | |
def ip_type(): | |
minion_config = salt.config.minion_config('/etc/salt/minion') | |
minion_grains = salt.loader.grains(minion_config) | |
grains = {} | |
ip_addresses = minion_grains.get('ipv4') | |
if ip_addresses: | |
for ip in ip_addresses: | |
f = unpack('!I',inet_pton(AF_INET,ip))[0] | |
private = ( | |
[ 3232235520, 4294901760 ], # 192.168.0.0, 255.255.0.0 http://tools.ietf.org/html/rfc1918 | |
[ 2886729728, 4293918720 ], # 172.16.0.0, 255.240.0.0 http://tools.ietf.org/html/rfc1918 | |
[ 167772160, 4278190080 ], # 10.0.0.0, 255.0.0.0 http://tools.ietf.org/html/rfc1918 | |
) | |
for net in private: | |
if (f & net[1]) == net[0]: | |
grains['local_ip'] = ip | |
else: | |
grains['public_ip'] = ip | |
return grains | |
if __name__ == "__main__": | |
print ip_type() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment