Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2016 01:22
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 anonymous/4bfc791bc876d71d34a5 to your computer and use it in GitHub Desktop.
Save anonymous/4bfc791bc876d71d34a5 to your computer and use it in GitHub Desktop.
#!/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