Skip to content

Instantly share code, notes, and snippets.

@pypykim
Created June 12, 2014 00:42
Show Gist options
  • Save pypykim/1cd205cb06152f9ac8ab to your computer and use it in GitHub Desktop.
Save pypykim/1cd205cb06152f9ac8ab to your computer and use it in GitHub Desktop.

Revisions

  1. pypykim revised this gist Jun 13, 2014. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -200,6 +200,9 @@ def get_ip_number_list(m, ip_number):
    # get_ip()
    # print get_host('74.125.235.192')#.__str__().find('ggpht')
    # dic_to_config(get_ip())
    print get_host('118.174.25.7')
    # print get_host('118.174.25.7')
    a = [1, 2]
    a = a + [3, 4]
    print a


  2. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -188,7 +188,7 @@ def get_ip_number_list(m, ip_number):
    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(netList[0].split('.')[:1])
    net_address('.'.join(netList[0].split('.')[:1]))



  3. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -188,7 +188,7 @@ def get_ip_number_list(m, ip_number):
    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()
    net_address(netList[0].split('.')[:1])



  4. pypykim revised this gist Jun 13, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -183,11 +183,12 @@ def get_ip_number_list(m, ip_number):
    netList = net.split('/')
    if int(netList[1]) == 24:
    ipList.append([netList[0][:netList[0].rindex('.')], 1, 254])
    elif int(netList[1]) < 24:
    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()



  5. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -187,7 +187,7 @@ def get_ip_number_list(m, ip_number):
    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 :
    for m in range(ip_number_list[0], ip_number_list[1]):



  6. pypykim revised this gist Jun 13, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -184,8 +184,10 @@ def get_ip_number_list(m, ip_number):
    if int(netList[1]) == 24:
    ipList.append([netList[0][:netList[0].rindex('.')], 1, 254])
    elif int(netList[1]) < 24:
    temp = 24 - int(netList[1])
    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 :



  7. pypykim revised this gist Jun 13, 2014. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -172,23 +172,24 @@ def dic_to_config(input_dict):

    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 int(netList[1]) < 24:
    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
    temp = 24 - int(netList[1])
    ip_number = int(netList[0].split('.')[2])




    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')
  8. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -185,6 +185,7 @@ def get_ip_number_list(m, ip_number):
    return [m * n, m * (n + 1)]
    n += 1
    temp = 24 - int(netList[1])
    ip_number = int(netList[0].split('.')[2])



  9. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -183,6 +183,7 @@ def get_ip_number_list(m, ip_number):
    while True:
    if m * n <= ip_number < m * (n + 1):
    return [m * n, m * (n + 1)]
    n += 1
    temp = 24 - int(netList[1])


  10. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -182,7 +182,7 @@ def get_ip_number_list(m, ip_number):
    temp = 2 ** m
    while True:
    if m * n <= ip_number < m * (n + 1):
    return []
    return [m * n, m * (n + 1)]
    temp = 24 - int(netList[1])


  11. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -181,7 +181,7 @@ def get_ip_number_list(m, ip_number):
    n = 0
    temp = 2 ** m
    while True:
    if 2 ** temp * n <= ip_number < 2 ** temp * (n + 1):
    if m * n <= ip_number < m * (n + 1):
    return []
    temp = 24 - int(netList[1])

  12. pypykim revised this gist Jun 13, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -177,10 +177,12 @@ def net_address(net_address_set):
    if int(netList[1]) == 24:
    ipList.append([netList[0][:netList[0].rindex('.')], 1, 254])
    elif int(netList[1]) < 24:
    def get_ip_number_list(temp, ip_number):
    def get_ip_number_list(m, ip_number):
    n = 0
    temp = 2 ** m
    while True:

    if 2 ** temp * n <= ip_number < 2 ** temp * (n + 1):
    return []
    temp = 24 - int(netList[1])


  13. pypykim revised this gist Jun 13, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -177,6 +177,11 @@ def net_address(net_address_set):
    if int(netList[1]) == 24:
    ipList.append([netList[0][:netList[0].rindex('.')], 1, 254])
    elif int(netList[1]) < 24:
    def get_ip_number_list(temp, ip_number):
    n = 0
    while True:

    temp = 24 - int(netList[1])



  14. pypykim revised this gist Jun 13, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -173,9 +173,9 @@ def dic_to_config(input_dict):
    def net_address(net_address_set):
    ipList = []
    for net in net_address_set:
    tempList = net.split('/')
    if int(tempList[1]) == 24:
    ipList.append([tempList[0][:tempList[0].rindex('.')], 1, 254])
    netList = net.split('/')
    if int(netList[1]) == 24:
    ipList.append([netList[0][:netList[0].rindex('.')], 1, 254])
    elif int(netList[1]) < 24:


  15. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -175,7 +175,7 @@ def net_address(net_address_set):
    for net in net_address_set:
    tempList = net.split('/')
    if int(tempList[1]) == 24:
    return [tempList[0][:tempList[0].rindex('.')], 1, 254]
    ipList.append([tempList[0][:tempList[0].rindex('.')], 1, 254])
    elif int(netList[1]) < 24:


  16. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -171,7 +171,7 @@ def dic_to_config(input_dict):


    def net_address(net_address_set):

    ipList = []
    for net in net_address_set:
    tempList = net.split('/')
    if int(tempList[1]) == 24:
  17. pypykim revised this gist Jun 13, 2014. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -171,10 +171,13 @@ def dic_to_config(input_dict):


    def net_address(net_address_set):

    for net in net_address_set:
    netList = net.split('/')
    if int(netList[1]) == 24:
    return [netList[0][:netList[0].rindex('.')], 1, 254]
    tempList = net.split('/')
    if int(tempList[1]) == 24:
    return [tempList[0][:tempList[0].rindex('.')], 1, 254]
    elif int(netList[1]) < 24:



    if __name__ == '__main__':
  18. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -174,7 +174,7 @@ def net_address(net_address_set):
    for net in net_address_set:
    netList = net.split('/')
    if int(netList[1]) == 24:
    return [netList[:netList[0].rindex('.')], 1, 254]
    return [netList[0][:netList[0].rindex('.')], 1, 254]


    if __name__ == '__main__':
  19. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -174,7 +174,7 @@ def net_address(net_address_set):
    for net in net_address_set:
    netList = net.split('/')
    if int(netList[1]) == 24:

    return [netList[:netList[0].rindex('.')], 1, 254]


    if __name__ == '__main__':
  20. pypykim revised this gist Jun 13, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -170,7 +170,7 @@ def dic_to_config(input_dict):
    config.write(open('new_host_file', 'w'))


    def net_to_ip(net_address_set):
    def net_address(net_address_set):
    for net in net_address_set:
    netList = net.split('/')
    if int(netList[1]) == 24:
  21. pypykim revised this gist Jun 13, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -170,10 +170,10 @@ def dic_to_config(input_dict):
    config.write(open('new_host_file', 'w'))


    def net_to_ip(net_list):
    for net in net_list:
    tempList = net.split('/')
    if int(net.split('/')[1]) == 24:
    def net_to_ip(net_address_set):
    for net in net_address_set:
    netList = net.split('/')
    if int(netList[1]) == 24:



  22. pypykim revised this gist Jun 13, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -170,6 +170,12 @@ def dic_to_config(input_dict):
    config.write(open('new_host_file', 'w'))


    def net_to_ip(net_list):
    for net in net_list:
    tempList = net.split('/')
    if int(net.split('/')[1]) == 24:



    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']
  23. pypykim revised this gist Jun 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -178,6 +178,6 @@ def dic_to_config(input_dict):
    # get_ip()
    # print get_host('74.125.235.192')#.__str__().find('ggpht')
    # dic_to_config(get_ip())
    print get_host('123.205.250.145')
    print get_host('118.174.25.7')


  24. pypykim revised this gist Jun 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ def get_host(ip_address):
    tempList = str(e).split("'")
    nameList.append(tempList[-2])
    return nameList
    return
    return e
    except Timeout as e:
    print ip_address + ' time out'
    return -3
  25. pypykim revised this gist Jun 12, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion google_temp.py
    Original file line number Diff line number Diff line change
    @@ -177,6 +177,7 @@ def dic_to_config(input_dict):
    # 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())
    # dic_to_config(get_ip())
    print get_host('123.205.250.145')


  26. pypykim created this gist Jun 12, 2014.
    182 changes: 182 additions & 0 deletions google_temp.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,182 @@
    #!/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
    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'))



    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())