#!/usr/bin/env python #coding:utf-8 import sys from GCC import ip_set usage = ''' get_host.py usage: -h, --help, print help message get_host.py [ip_address], for example, get_host.py 192.168.1.1 ''' def get_host(ip_address): try: import requests except ImportError as e: print "Please 'pip install requests'" return -1 try: from requests.exceptions import SSLError, Timeout headers='' requests.get('https://' + ip_address, timeout=2) return -2 except SSLError as e: nameList = [] if "', '" in str(e): nameList = str(e).split("', '") nameList[0] = nameList[0].split("'")[-1] nameList[-1] = nameList[-1].split("'")[0] return nameList elif 'match' in str(e): tempList = str(e).split("'") nameList.append(tempList[-2]) return nameList return e except Timeout as e: print ip_address + ' time out' return -3 except Exception as e: print ip_address + '\n' + str(e) return -4 # if __name__ == '__main__': # if sys.argv[1] in ('-h', '--help') or len(sys.argv) != 2: # print usage # sys.exit() # else: # print get_host(sys.argv[1]) # print get_host('74.125.20.31') import re def read_hosts(): nameList = [] ipList = [] with open('hosts.txt', 'r') as f: for line in f: if re.match(r'[0-9]', line[0]) is not None and line[0:3] != '127': lineList = re.split(r'\s+', line) nameList.append(lineList[1]) ipList.append(lineList[0]) nameList = sorted(set(nameList), key=nameList.index) ipList = sorted(set(ipList), key=ipList.index) return [nameList, ipList] def write_name(nameList): with open('hosts.conf', 'w') as f: for name in nameList: f.writelines(name + '\n') # write_name(read_hosts()) import ConfigParser def config_hosts(): config = ConfigParser.RawConfigParser() from dns.resolver import dns def get_dns(): name_server='1.2.4.8' ADDITIONAL_RDCLASS = 65535 request = dns.message.make_query('photos-ugc.l.googleusercontent.com', dns.rdatatype.ANY) request.flags |= dns.flags.AD request.find_rrset(request.additional, dns.name.root, ADDITIONAL_RDCLASS, dns.rdatatype.OPT, create=True, force_unique=True) response = dns.query.udp(request, name_server) print response # get_dns() def ping_host(): ipList = read_hosts()[1] newipList =[] for ip in ipList: if isinstance(get_host(ip), int): newipList.append(ip) print newipList return newipList def get_name(ipList): newList = [] with open('hosts.txt','r') as f: for line in f: for ip in ipList: lineList = re.split(r'\s+', line) if ip == lineList[0]: newList.append(re.split(r'\n', line)[0]) return newList def list_to_file(the_list, file_name): with open(file_name, 'w') as f: for c in the_list: f.writelines(c + '\n') def get_ip(): androidList = [] ggphtList = [] gstaticList = [] googleapisList = [] talkList = [] googleusercontentList = [] googlecodeList = [] googlegroupsList = [] googlevideoList = [] googlesourceList = [] for ip_tuple in ip_set: for nu in range(ip_tuple[1], ip_tuple[2] + 1): ip = ip_tuple[0] + '.' + str(nu) tempList = get_host(ip) if str(tempList).find('android.com') != -1: androidList.append(ip) if str(tempList).find('ggpht') != -1: ggphtList.append(ip) if str(tempList).find('gstatic.com') != -1: gstaticList.append(ip) if str(tempList).find('googleapis') != -1: googleapisList.append(ip) if str(tempList).find('talk') != -1: talkList.append(ip) if str(tempList).find('googleusercontent') != -1: googleusercontentList.append(ip) if str(tempList).find('googlecode') != -1: googlecodeList.append(ip) if str(tempList).find('googlesource') != -1: googlesourceList.append(ip) if str(tempList).find('googlevideo') != -1: googlevideoList.append(ip) if str(tempList).find('googlegroups') != -1: googlegroupsList.append(ip) return { 'androidList' : androidList, 'ggphtList' : ggphtList, 'gstaticList' : gstaticList, 'googleapisList' : googleapisList, 'talkList' : talkList, 'googleusercontentList' : googleusercontentList, 'googlecodeList' : googlecodeList, 'googlegroupsList' : googlegroupsList, 'googlevideoList' : googlevideoList, 'googlesourceList' : googlesourceList } def dic_to_config(input_dict): config = ConfigParser.RawConfigParser() for key, value in input_dict.items(): config.add_section(key) for c in value: config.set(key, key, c) config.write(open('new_host_file', 'w')) def net_address(net_address_set): ipList = [] def get_ip_number_list(m, ip_number): n = 0 temp = 2 ** m while True: if m * n <= ip_number < m * (n + 1): return [m * n, m * (n + 1)] n += 1 for net in net_address_set: netList = net.split('/') if int(netList[1]) == 24: ipList.append([netList[0][:netList[0].rindex('.')], 1, 254]) elif 16 < int(netList[1]) < 24: m = 24 - int(netList[1]) ip_number = int(netList[0].split('.')[2]) ip_number_list = get_ip_number_list(m, ip_number) for m in range(ip_number_list[0], ip_number_list[1]): net_address('.'.join(netList[0].split('.')[:1])) if __name__ == '__main__': # the_list = ['74.125.22.113', '74.125.22.189', '173.194.37.207', '74.125.20.129', '173.194.37.99', '173.194.76.128'] # list_to_file(get_name(ping_host()), 'list_file') # print get_host('61.219.131.251').__str__().find('mail') # get_ip() # print get_host('74.125.235.192')#.__str__().find('ggpht') # dic_to_config(get_ip()) # print get_host('118.174.25.7') a = [1, 2] a = a + [3, 4] print a