Python socket.gethostbyname_ex() Examples

The following are code examples for showing how to use socket.gethostbyname_ex(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: cerberus-core   Author: ovh   File: utils.py    (license) View Source Project 7 votes vote down vote up
def get_ips_from_url(url):
    """
        Retrieve IPs from url

        :param str url: The url to resolve
        :rtype: list
        :return: the list of resolved IP address for given url
    """
    try:
        parsed = urlparse(url)
        if parsed.hostname:
            socket.setdefaulttimeout(5)
            ips = socket.gethostbyname_ex(parsed.hostname)[2]
            return ips
    except (ValueError, socket.error, socket.gaierror, socket.herror, socket.timeout):
        pass 
Example 2
Project: Intranet-Penetration   Author: yuxiaokui   File: proxylib.py    (license) View Source Project 7 votes vote down vote up
def gethostbyname2(self, hostname):
        try:
            iplist = self.dns_cache[hostname]
        except KeyError:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif self.dns_servers:
                try:
                    record = dnslib_resolve_over_udp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                except socket.gaierror:
                    record = dnslib_resolve_over_tcp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                iplist = dnslib_record2iplist(record)
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            self.dns_cache[hostname] = iplist
        return iplist 
Example 3
Project: llk   Author: Tycx2ry   File: sflib.py    (license) View Source Project 6 votes vote down vote up
def checkDnsWildcard(self, target):
        randpool = 'bcdfghjklmnpqrstvwxyz3456789'
        randhost = ''.join([random.choice(randpool) for x in range(10)])

        # An exception will be raised if the resolution fails
        try:
            addrs = socket.gethostbyname_ex(randhost + "." + target)
            self.debug(target + " has wildcard DNS.")
            return True
        except BaseException as e:
            self.debug(target + " does not have wildcard DNS.")
            return False

    # Scrape Google for content, starting at startUrl and iterating through
    # results based on options supplied. Will return a dictionary of all pages
    # fetched and their contents {page => content}.
    # Options accepted:
    # limit: number of search result pages before returning, default is 10
    # nopause: don't randomly pause between fetches
    # useragent: User-Agent string to use
    # timeout: Fetch timeout 
Example 4
Project: spiderfoot   Author: wi-fi-analyzer   File: sfp_dns.py    (license) View Source Project 6 votes vote down vote up
def resolveHost(self, hostname):
        if hostname in self.resolveCache:
            self.sf.debug("Returning cached result for " + hostname + " (" +
                          str(self.resolveCache[hostname]) + ")")
            return self.resolveCache[hostname]

        try:
            addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(hostname))
            self.resolveCache[hostname] = addrs
            self.sf.debug("Resolved " + hostname + " to: " + str(addrs))
            return addrs
        except BaseException as e:
            self.sf.debug("Unable to resolve " + hostname + " (" + str(e) + ")")
            return list()

    # Resolve a host to IPv6 
Example 5
Project: spiderfoot   Author: wi-fi-analyzer   File: sfp_sharedip.py    (license) View Source Project 6 votes vote down vote up
def validateIP(self, host, ip):
        try:
            addrs = socket.gethostbyname_ex(host)
        except BaseException as e:
            self.sf.debug("Unable to resolve " + host + ": " + str(e))
            return False

        for addr in addrs:
            if type(addr) == list:
                for a in addr:
                    if str(a) == ip:
                        return True
            else:
                if str(addr) == ip:
                    return True
        return False

    # Handle events sent to this module 
Example 6
Project: spiderfoot   Author: wi-fi-analyzer   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 7
Project: bedrock-core   Author: Bedrock-py   File: models.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, name, rootdir, src_id, src_type, time,
                 ingest_id, group_name, matrices=None,
                 status=None, count=0, stash=None, filepath=None):
        self.name = name
        self.host = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
                     if not ip.startswith("127.")][-1]
        self.rootdir = rootdir
        self.src_id = src_id
        self.src_type = src_type
        self.created = time
        self.ingest_id = ingest_id
        self.matrices = none2empty(matrices)
        self.status = none2empty(status)
        self.count = count
        self.stash = none2empty(stash)
        self.group_name = group_name
        self.filepath = filepath 
Example 8
Project: Intranet-Penetration   Author: yuxiaokui   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def get_country_code(self, hostname, dnsservers):
        """http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
        try:
            return self.region_cache[hostname]
        except KeyError:
            pass
        try:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif dnsservers:
                iplist = dnslib_record2iplist(dnslib_resolve_over_udp(hostname, dnsservers, timeout=2))
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            if iplist[0].startswith(('127.', '192.168.', '10.')):
                country_code = 'LOCAL'
            else:
                country_code = self.geoip.country_code_by_addr(iplist[0])
        except StandardError as e:
            logging.warning('DirectRegionFilter cannot determine region for hostname=%r %r', hostname, e)
            country_code = ''
        self.region_cache[hostname] = country_code
        return country_code 
Example 9
Project: MKFQ   Author: maojingios   File: proxy_handler.py    (license) View Source Project 6 votes vote down vote up
def is_local(self, hosts):
        if 0 == len(self.local_names):
            self.local_names.append('localhost')
            self.local_names.append(socket.gethostname().lower());
            try:
                self.local_names.append(socket.gethostbyname_ex(socket.gethostname())[-1])
            except socket.gaierror:
                # TODO Append local IP address to local_names
                pass

        for s in hosts:
            s = s.lower()
            if s.startswith('127.') \
                    or s.startswith('192.168.') \
                    or s.startswith('10.') \
                    or s.startswith('169.254.') \
                    or s in self.local_names:
                print s
                return True
        return False 
Example 10
Project: hakkuframework   Author: 4shadoww   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 11
Project: bigquery-bokeh-dashboard   Author: GoogleCloudPlatform   File: utils.py    (license) View Source Project 6 votes vote down vote up
def _resync(self):
        """
        Check if the list of available nodes has changed. If any change is
        detected, a new HashClient pointing to all currently available
        nodes is returned, otherwise the current client is returned.
        """
        # Collect the all Memcached pods' IP addresses
        try:
            _, _, ips = socket.gethostbyname_ex(self.host)
        except socket.gaierror:
            # The host could not be found. This mean that either the service is
            # down or that no pods are running
            ips = []
        if set(ips) != set(self._ips):
            # A different list of ips has been detected, so we generate
            # a new client
            self._ips = ips
            if self._ips:
                servers = [(ip, self.port) for ip in self._ips]
                self._client = HashClient(servers, use_pooling=True)
            else:
                self._client = None 
Example 12
Project: sslstrip-hsts-openwrt   Author: adde88   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 13
Project: OpenDoor   Author: stanislav-web   File: socks.py    (license) View Source Project 6 votes vote down vote up
def get_ips_addresses(host):
        """
        Get remote ip addresses
        :param str host: target host
        :return: list
        """

        try:
            _, _, ips_list = socket.gethostbyname_ex(host)
            if not ips_list:
                ips = ''
            else:
                ips = '['+', '.join(ips_list) +']'
        except socket.gaierror:
            ips = ''
        return ips 
Example 14
Project: cheapstream   Author: miltador   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 15
Project: ngas   Author: ICRAR   File: ngamsServer.py    (license) View Source Project 6 votes vote down vote up
def is_it_us(self, host, port):
        """
        True if the host/port combination corresponds to an address exposed by
        this server
        """
        if port != self.portNo:
            return False

        our_ip = self.ipAddress
        if our_ip != '0.0.0.0' and our_ip == host:
            return True

        # We are exposed to all interfaces and `host` might be one of them
        # gethostbyname_ex[2] is a list of addresses
        for h in socket.gethostbyname_ex(host)[2]:
            if h in self.all_ip_addresses:
                return True
        return False 
Example 16
Project: MyToolKit   Author: 3xp10it   File: my_bing_domains_v1_alone.py    (license) View Source Project 6 votes vote down vote up
def main():    
    if len(sys.argv)!=2:
        usage()
        sys.exit(0)
    
    try:
        file=open(sys.argv[1],"r+")
        target_list=file.readlines()
        file.close()
        true_target_list=[]
        true_target_list=get_pure_list(target_list)
        for each in true_target_list:
            try:
                get_the_one_target_domains(each)
            except:
                print "getip or socket.gethostbyname_ex wrong,the site may break down,you can check it."
    except:
        sharedHost=sys.argv[1]
        get_the_one_target_domains(sharedHost)
        print "open file wrong coz this input is not a file but a domain or when you do put a file,then the script open file wrong" 
Example 17
Project: Infrax-as-Code-1000-webservers-in-40-minutes   Author: ezeeetm   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 18
Project: xxNet   Author: drzorm   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def get_country_code(self, hostname, dnsservers):
        """http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
        try:
            return self.region_cache[hostname]
        except KeyError:
            pass
        try:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif dnsservers:
                iplist = dnslib_record2iplist(dnslib_resolve_over_udp(hostname, dnsservers, timeout=2))
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            if iplist[0].startswith(('127.', '192.168.', '10.')):
                country_code = 'LOCAL'
            else:
                country_code = self.geoip.country_code_by_addr(iplist[0])
        except StandardError as e:
            logging.warning('DirectRegionFilter cannot determine region for hostname=%r %r', hostname, e)
            country_code = ''
        self.region_cache[hostname] = country_code
        return country_code 
Example 19
Project: xxNet   Author: drzorm   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def gethostbyname2(self, hostname):
        try:
            iplist = self.dns_cache[hostname]
        except KeyError:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif self.dns_servers:
                try:
                    record = dnslib_resolve_over_udp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                except socket.gaierror:
                    record = dnslib_resolve_over_tcp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                iplist = dnslib_record2iplist(record)
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            self.dns_cache[hostname] = iplist
        return iplist 
Example 20
Project: pycam   Author: SebKuzminsky   File: __init__.py    (license) View Source Project 6 votes vote down vote up
def get_all_ips():
    """ try to get all IPs of this machine

    The resulting list of IPs contains non-local IPs first, followed by
    local IPs (starting with "127....").
    """
    def get_ips_of_name(name):
        try:
            ips = socket.gethostbyname_ex(name)
            if len(ips) == 3:
                return ips[2]
        except socket.gaierror:
            return []
    result = []
    result.extend(get_ips_of_name(socket.gethostname()))
    result.extend(get_ips_of_name("localhost"))
    filtered_result = []
    for one_ip in result:
        if one_ip not in filtered_result:
            filtered_result.append(one_ip)
    # non-local IPs first
    filtered_result.sort(key=lambda ip: ((1 if ip.startswith("127.") else 0), ip))
    return filtered_result 
Example 21
Project: djangocon-2016-demo   Author: hadjango   File: __init__.py    (license) View Source Project 6 votes vote down vote up
def ip():
    """The ip where the addons site can be accessed"""
    docker_host = os.environ.get("DOCKER_HOST") or ""
    if not docker_host:
        with quiet():
            docker_env = local("docker-machine env addons", capture=True)
        if docker_env:
            match = re.search(r'DOCKER_HOST="(tcp://[^"]+?)"', docker_env)
            if match:
                docker_host = match.group(1)

    match = re.search(r'tcp://([^:]+):', docker_host)
    if match:
        print(match.group(1))
    else:
        try:
            # host used by dlite
            _, _, ips = socket.gethostbyname_ex("local.docker")
        except:
            abort("Could not determine docker-machine host; perhaps localhost?")
        else:
            print(ips[0]) 
Example 22
Project: kekescan   Author: xiaoxiaoleo   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 23
Project: kekescan   Author: xiaoxiaoleo   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 24
Project: dymo-m10-python   Author: pbrf   File: index.py    (license) View Source Project 6 votes vote down vote up
def mirrors(self):
        """
        Return the list of hostnames which are mirrors for this index.
        :return: A (possibly empty) list of hostnames of mirrors.
        """
        result = []
        try:
            host = socket.gethostbyname_ex(self.mirror_host)[0]
        except socket.gaierror: # pragma: no cover
            host = None
        if host:
            last, rest = host.split('.', 1)
            n = len(last)
            host_list = (''.join(w) for w in itertools.chain.from_iterable(
                        itertools.product(ascii_lowercase, repeat=i)
                        for i in range(1, n + 1)))
            for s in host_list:
                result.append('.'.join((s, rest)))
                if s == last:
                    break
        return result 
Example 25
Project: pypi-legacy   Author: pypa   File: mirrorlib.py    (license) View Source Project 6 votes vote down vote up
def _mirror_list(first):
    '''Generator producing all mirror names'''
    ord_a = ord('a')
    last = socket.gethostbyname_ex('last.pypi.python.org')
    cur_index = ord(first)-ord_a
    cur = first+'.pypi.python.org'
    while True:
        for family, _, _, _, sockaddr in socket.getaddrinfo(cur, 0, 0, socket.SOCK_STREAM):
            yield cur, family, sockaddr
        if last[0] == cur:
            break
        cur_index += 1
        if cur_index < 26:
            # a..z
            cur = chr(ord_a+cur_index)
        elif cur_index > 701:
            raise ValueError, 'too many mirrors'
        else:
            # aa, ab, ... zz
            cur = divmod(cur_index, 26)
            cur = chr(ord_a-1+cur[0])+chr(ord_a+cur[1])
        cur += '.pypi.python.org' 
Example 26
Project: matlab_ctp_md_td   Author: xunquant   File: ip_and_socket.py    (license) View Source Project 6 votes vote down vote up
def get_local_ip(platform_category):
	'''
	????IP??
	'''
	
	nic_dict=get_active_nic_name()
	
	major_nic_name=nic_dict.keys()[0]
	
	if platform_category.__contains__('windows'):#???windows????
		host_name=socket.gethostname()
		my_host_name=socket.getfqdn()
		#????ip
		ip_list=socket.gethostbyname_ex(my_host_name)
		my_local_ip=socket.gethostbyname(my_host_name)
	else:#linux??
		try:
			my_local_ip=get_ip_of_a_net_adapter_in_linux(major_nic_name)#????????????,????eth0
		except:
			my_local_ip='??????????????????????'
			print my_local_ip
		pass
	return my_local_ip
	pass 
Example 27
Project: minihydra   Author: VillanCh   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 28
Project: probesc   Author: bstaint   File: net.py    (license) View Source Project 6 votes vote down vote up
def valid_ip(host):
    ''' lookup address with gethostbyname '''
    ipaddr = None
    fake_domain = 'notexistsfuckispsbbaidu.com'

    try:
        # ??ISP??
        if not hasattr(valid_ip, 'fake_ip'):
            try: valid_ip.fake_ip = socket.gethostbyname(fake_domain)
            except socket.error: valid_ip.fake_ip = None

        _,_,ipaddrs = socket.gethostbyname_ex(host)

        if ipaddrs[0] != valid_ip.fake_ip:
            ipaddr = ipaddrs[0]
    except socket.error:
        pass
    return ipaddr 
Example 29
Project: 00scanner   Author: xiaoqin00   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 30
Project: wechat-sendall   Author: vonnyfly   File: wechat-sendall.py    (license) View Source Project 6 votes vote down vote up
def getQRImage():
  path = os.path.join(os.getcwd(), "qrcode.jpg")
  url = "https://login.weixin.qq.com/qrcode/" + uuid
  r = s.get(url, stream=True)
  # debugReq(r)
  if r.status_code == 200:
    with open(path, 'wb') as f:
      r.raw.decode_content = True
      shutil.copyfileobj(r.raw, f)
  import socket
  ip = [l
        for l in
        ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
          if not ip.startswith("127.")][:1],
         [[(sc.connect(('8.8.8.8', 80)), sc.getsockname()[0], sc.close())
           for sc in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]])
        if l][0][0]
  print "[+] Please open http://" + ip + ":" + str(http_port) + "/qrcode.jpg or open " + path

  time.sleep(1) 
Example 31
Project: dnszonetest   Author: maartenq   File: main.py    (license) View Source Project 6 votes vote down vote up
def get_nameserver_ip(self):
        '''
        Get Resolver object depending on self.nameserver
        '''
        if self.nameserver is None:
            logger.debug('Get IP number(s) of system resolvers')
            self.nameserver_ip = \
                dns.resolver.get_default_resolver().nameservers[0]
        else:
            try:
                self.nameserver_ip = socket.gethostbyname_ex(
                    self.nameserver)[2][0]
            except socket.gaierror as err:
                raise UnableToResolveNameServerException(
                    'Unable to resolve nameserver "{0}". {1}'.format(
                        self.nameserver,
                        err
                    )
                ) 
Example 32
Project: wechat_sender   Author: bluedazzle   File: sender.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, name=None, token=None, receiver=None, host='http://localhost', port=10245, level=30):
        """
        :param name:  (??|str) - ???????????????????????
        :param token: (??|str) - ???????????? listen ?? token ??
        :param receiver: (??|str) - ????wxpy ? puid ? ?????????????? default_receiver
        :param host: (??|str) - ?????????????
        :param port: (??|int) - ??????? 10245 ??????????? listen ?? port ??
        :param level: (??|int) - ?????????? logging.WARNING
        """
        super(LoggingSenderHandler, self).__init__(level)
        Sender.__init__(self, token, receiver, host, port)
        if not name:
            import socket
            ip = socket.gethostbyname_ex(socket.gethostname())[0]
            name = ip
        self.name = name
        self.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')) 
Example 33
Project: mysplunk_csc   Author: patel-bhavin   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 34
Project: deb-python-eventlet   Author: openstack   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 35
Project: Docker-XX-Net   Author: kuanghy   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def get_country_code(self, hostname, dnsservers):
        """http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
        try:
            return self.region_cache[hostname]
        except KeyError:
            pass
        try:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif dnsservers:
                iplist = dnslib_record2iplist(dnslib_resolve_over_udp(hostname, dnsservers, timeout=2))
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            if iplist[0].startswith(('127.', '192.168.', '10.')):
                country_code = 'LOCAL'
            else:
                country_code = self.geoip.country_code_by_addr(iplist[0])
        except StandardError as e:
            logging.warning('DirectRegionFilter cannot determine region for hostname=%r %r', hostname, e)
            country_code = ''
        self.region_cache[hostname] = country_code
        return country_code 
Example 36
Project: Docker-XX-Net   Author: kuanghy   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def gethostbyname2(self, hostname):
        try:
            iplist = self.dns_cache[hostname]
        except KeyError:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif self.dns_servers:
                try:
                    record = dnslib_resolve_over_udp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                except socket.gaierror:
                    record = dnslib_resolve_over_tcp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                iplist = dnslib_record2iplist(record)
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            self.dns_cache[hostname] = iplist
        return iplist 
Example 37
Project: inwx-zonefile-sync   Author: lukas2511   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 38
Project: Proxy-Factory   Author: ping99   File: proxylib.py.bak.py    (license) View Source Project 6 votes vote down vote up
def get_country_code(self, hostname, dnsservers):
        """http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
        try:
            return self.region_cache[hostname]
        except KeyError:
            pass
        try:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif dnsservers:
                iplist = dnslib_record2iplist(dnslib_resolve_over_udp(hostname, dnsservers, timeout=2))
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            if iplist[0].startswith(('127.', '192.168.', '10.')):
                country_code = 'LOCAL'
            else:
                country_code = self.geoip.country_code_by_addr(iplist[0])
        except StandardError as e:
            logging.warning('DirectRegionFilter cannot determine region for hostname=%r %r', hostname, e)
            country_code = ''
        self.region_cache[hostname] = country_code
        return country_code 
Example 39
Project: Proxy-Factory   Author: ping99   File: proxylib.py.bak.py    (license) View Source Project 6 votes vote down vote up
def gethostsbyname(self, hostname):
        try:
            iplist = self.dns_cache[hostname]
        except KeyError:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif self.dns_servers:
                try:
                    record = dnslib_resolve_over_udp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                except socket.gaierror:
                    record = dnslib_resolve_over_tcp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                iplist = dnslib_record2iplist(record)
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            self.dns_cache[hostname] = iplist
        return iplist 
Example 40
Project: Proxy-Factory   Author: ping99   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def get_country_code(self, hostname, dnsservers):
        """http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
        try:
            return self.region_cache[hostname]
        except KeyError:
            pass
        try:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif dnsservers:
                iplist = dnslib_record2iplist(dnslib_resolve_over_udp(hostname, dnsservers, timeout=2))
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            if iplist[0].startswith(('127.', '192.168.', '10.')):
                country_code = 'LOCAL'
            else:
                country_code = self.geoip.country_code_by_addr(iplist[0])
        except StandardError as e:
            logging.warning('DirectRegionFilter cannot determine region for hostname=%r %r', hostname, e)
            country_code = ''
        self.region_cache[hostname] = country_code
        return country_code 
Example 41
Project: Proxy-Factory   Author: ping99   File: proxylib.py    (license) View Source Project 6 votes vote down vote up
def gethostsbyname(self, hostname):
        try:
            iplist = self.dns_cache[hostname]
        except KeyError:
            if re.match(r'^\d+\.\d+\.\d+\.\d+$', hostname) or ':' in hostname:
                iplist = [hostname]
            elif self.dns_servers:
                try:
                    record = dnslib_resolve_over_udp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                except socket.gaierror:
                    record = dnslib_resolve_over_tcp(hostname, self.dns_servers, timeout=2, blacklist=self.dns_blacklist)
                iplist = dnslib_record2iplist(record)
            else:
                iplist = socket.gethostbyname_ex(hostname)[-1]
            self.dns_cache[hostname] = iplist
        return iplist 
Example 42
Project: spiderfoot   Author: ParrotSec   File: sfp_dns.py    (license) View Source Project 6 votes vote down vote up
def resolveHost(self, hostname):
        if hostname in self.resolveCache:
            self.sf.debug("Returning cached result for " + hostname + " (" +
                          str(self.resolveCache[hostname]) + ")")
            return self.resolveCache[hostname]

        try:
            addrs = self.sf.normalizeDNS(socket.gethostbyname_ex(hostname))
            self.resolveCache[hostname] = addrs
            self.sf.debug("Resolved " + hostname + " to: " + str(addrs))
            return addrs
        except BaseException as e:
            self.sf.debug("Unable to resolve " + hostname + " (" + str(e) + ")")
            return list()

    # Resolve a host to IPv6 
Example 43
Project: spiderfoot   Author: ParrotSec   File: sfp_sharedip.py    (license) View Source Project 6 votes vote down vote up
def validateIP(self, host, ip):
        try:
            addrs = socket.gethostbyname_ex(host)
        except BaseException as e:
            self.sf.debug("Unable to resolve " + host + ": " + str(e))
            return False

        for addr in addrs:
            if type(addr) == list:
                for a in addr:
                    if str(a) == ip:
                        return True
            else:
                if str(addr) == ip:
                    return True
        return False

    # Handle events sent to this module 
Example 44
Project: spiderfoot   Author: ParrotSec   File: resolver.py    (license) View Source Project 6 votes vote down vote up
def override_system_resolver(resolver=None):
    """Override the system resolver routines in the socket module with
    versions which use dnspython's resolver.

    This can be useful in testing situations where you want to control
    the resolution behavior of python code without having to change
    the system's resolver settings (e.g. /etc/resolv.conf).

    The resolver to use may be specified; if it's not, the default
    resolver will be used.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    """
    if resolver is None:
        resolver = get_default_resolver()
    global _resolver
    _resolver = resolver
    socket.getaddrinfo = _getaddrinfo
    socket.getnameinfo = _getnameinfo
    socket.getfqdn = _getfqdn
    socket.gethostbyname = _gethostbyname
    socket.gethostbyname_ex = _gethostbyname_ex
    socket.gethostbyaddr = _gethostbyaddr 
Example 45
Project: JSParser   Author: nahamsec   File: safeurl.py    (license) View Source Project 6 votes vote down vote up
def resolveHostname(hostname):
        """
        Resolve a hostname to its IP(s)

        :param arg1: hostname
        :type arg1: string

        :rtype: list (string)
        """
        try:
            ips = gethostbyname_ex(hostname)
            return ips[2]
        except:
            raise InvalidDomainException(
                "Provided hostname 'hostname' doesn't \
                to an IP address") 
Example 46
Project: zbx   Author: karlesnine   File: zbx.py    (license) View Source Project 6 votes vote down vote up
def create_a_host(fqdn):
    """Create a host in zabbix server."""
    template_os_linux = get_template_id("Template OS Linux")
    if template_os_linux == "not found":
        click.echo('template not found in Zabbix : %s' % template_os_linux)
        sys.exit(0)
    else:
        dns_data = socket.gethostbyname_ex(fqdn)
        ip = dns_data[2][0]
        response = zapi.host.create(
            host=fqdn,
            interfaces={"type": 1, "main": 1, "useip": 0, "ip": ip, "dns": fqdn, "port": "10050"},
            groups={"groupid": "5"},
            templates={"templateid": template_os_linux}
        )
        click.echo('%s id %s is added with basic linux template' % (fqdn, response["hostids"][0])) 
Example 47
Project: kinect-2-libras   Author: inessadl   File: urllib2.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def get_names(self):
        if FileHandler.names is None:
            try:
                FileHandler.names = tuple(
                    socket.gethostbyname_ex('localhost')[2] +
                    socket.gethostbyname_ex(socket.gethostname())[2])
            except socket.gaierror:
                FileHandler.names = (socket.gethostbyname('localhost'),)
        return FileHandler.names

    # not entirely sure what the rules are here 
Example 48
Project: PyJFuzz   Author: mseclab   File: pjf_worker.py    (MIT License) View Source Project 5 votes vote down vote up
def get_urls(self):
        try:
            ip = ([l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not
            ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in
                                          [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0])
            yield "http://{0}:{1}/fuzzer.html".format(ip,self.config.ports["servers"]["HTTP_PORT"])
        except Exception as e:
            raise PJFBaseException(e.message if hasattr(e, "message") else str(e)) 
Example 49
Project: mongoaudit   Author: Exploit-install   File: tools.py    (license) View Source Project 5 votes vote down vote up
def try_address(fqdn):
    """
    Check if the fqdn is valid
    Args:
        fqdn (str): fully qualified domain name
    """
    import socket
    try:
        socket.gethostbyname_ex(fqdn)
    except (socket.gaierror, UnicodeEncodeError):
        return False
    else:
        return True 
Example 50
Project: hostapd-mana   Author: adde88   File: resolver.py    (license) View Source Project 5 votes vote down vote up
def restore_system_resolver():
    """Undo the effects of override_system_resolver().
    """
    global _resolver
    _resolver = None
    socket.getaddrinfo = _original_getaddrinfo
    socket.getnameinfo = _original_getnameinfo
    socket.getfqdn = _original_getfqdn
    socket.gethostbyname = _original_gethostbyname
    socket.gethostbyname_ex = _original_gethostbyname_ex
    socket.gethostbyaddr = _original_gethostbyaddr