Created
June 11, 2014 06:44
-
-
Save pypykim/af5a87bf02da8950f902 to your computer and use it in GitHub Desktop.
google_ip_filter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #coding:utf-8 | |
| import sys | |
| 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 | |
| except Timeout as e: | |
| print ip_address + ' time out' | |
| return -3 | |
| except Exception as e: | |
| print ip_address + '\n' + 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') | |
| 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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment