Merge pull request #9172 from s0undt3ch/hotfix/no-need-for-requests

The `stdlib` is good enough. Check additional IP information providers
This commit is contained in:
Joseph Hall 2013-12-11 04:12:03 -08:00
commit ea69592e76

View File

@ -1,20 +1,47 @@
try:
import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
# -*- coding: utf-8 -*-
'''
:codeauthor: Jeff Frost
:copyright: © 2013 by the SaltStack Team, see AUTHORS for more details.
:license: Apache 2.0, see LICENSE for more details.
salt.grains.external_ip
~~~~~~~~~~~~~~~~~~~~~~~
Return the external IP address reported by one of the following providers:
* ipecho.net
* externalip.net
* ident.me
Which ever reports a valid IP first
'''
# Import Python Libs
import urllib2
import contextlib
# Import salt libs
from salt.utils.validate.net import ipv4_addr as _ipv4_addr
def external_ip():
'''
Return the external IP address reported by ipecho.net
Return the external IP address
'''
check_ips = ('http://ipecho.net/plain',
'http://api.externalip.net/ip',
'http://v4.ident.me')
if not HAS_REQUESTS:
return {}
for url in check_ips:
try:
r = requests.get('http://ipecho.net/plain')
ip = r.content
except Exception:
ip = []
return {'external_ip': ip}
with contextlib.closing(urllib2.urlopen(url, timeout=3)) as req:
ip_ = req.read().strip()
if not _ipv4_addr(ip_):
continue
return {'external_ip': ip_}
except urllib2.HTTPError:
continue
# Return an empty value as a last resort
return {'external_ip': []}