Esempio n. 1
0
def _get_ext_ip():
    '''
    Return external IP of the host (master) executing salt-cloud
    :return: json external IP
    '''
    log.debug('Determining external IP...')

    check_ips = ('http://ipecho.net/plain', 'http://v4.ident.me')

    for url in check_ips:
        try:
            with contextlib.closing(urllib2.urlopen(url, timeout=3)) as req:
                ip_ = req.read().strip()
                if not _ipv4_addr(ip_):
                    continue
            log.debug('Found external IP {0}'.format(ip_))
            return {'external_ip': ip_}
        except (urllib2.HTTPError, urllib2.URLError, socket.timeout):
            log.error(
                'Error detecting external IP address\n\n'
                'The following exception was thrown during checking external IP of this machine ',
                urllib2.HTTPError,
                urllib2.URLError,
                socket.timeout,
                exc_info_on_loglevel=logging.DEBUG)
            continue
    # Return an empty value as a last resort
    return {'external_ip': []}
Esempio n. 2
0
def external_ip():
    '''
    Return the external IP address
    '''
    check_url = __opts__.get('external_ip.server', False)
    if check_url:
        try:
            timeout = __opts__.get('external_ip.timeout', 0.5)
            r = requests.get(check_url, timeout=timeout)
            ip_addr = r.json()
            if _ipv4_addr(ip_addr['ip_addr']):
                return {'external_ip': ip_addr['ip_addr']}
        except Timeout as exc:
            log.debug('Timeout exceeded: {0}'.format(exc))
        except (ConnectionError, HTTPError) as exc:
            log.debug('Connection error: {0}'.format(exc))

    return {'external_ip': None}
def ext_ip():
    '''
    Return the external IP address
    '''
    check_ips = ('http://ipecho.net/plain', 'http://v4.ident.me')

    for url in check_ips:
        try:
            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, urllib2.URLError, socket.timeout):
            continue

    # Return an empty value as a last resort
    return {'external_ip': []}
Esempio n. 4
0
def external_ip():
    '''
    Return the external IP address
    '''
    check_ips = ('http://ipecho.net/plain',
                 'http://api.externalip.net/ip',
                 'http://v4.ident.me')

    for url in check_ips:
        try:
            with contextlib.closing(_urlopen(url, timeout=3)) as req:
                ip_ = req.read().strip()
                if not _ipv4_addr(ip_):
                    continue
            return {'external_ip': ip_}
        except (HTTPError, URLError, socket.timeout):
            continue

    # Return an empty value as a last resort
    return {'external_ip': []}