This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
forked from gshmu/m2conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_goagent.py
191 lines (171 loc) · 6.04 KB
/
set_goagent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# auther Kim Kong kongqingzhang@gmail.com
import pdb
import sys
from GGC import net_address_set
import threading
import time
import socket
import ping
import requests
from requests.exceptions import SSLError, Timeout
try:
p = sys.argv[1]
except IndexError:
p = False
init_threading_count = threading.activeCount()
ipList = []
pyver2 = sys.version < '3'
def get_host(ip_address):
"""
test ip is connected to port 443
"""
try:
headers=''
requests.get('https://' + ip_address, timeout=2)
return -2
except SSLError as e:
name_list = []
ping_artt = ping.quiet_ping(ip_address)[2]
if "', '" in str(e):
name_list = str(e).split("', '")
name_list[0] = name_list[0].split("'")[-1]
name_list[-1] = name_list[-1].split("'")[0]
# print ip_address + '\n'
return [name_list, ip_address, ping_artt]
elif 'match' in str(e):
temp_list = str(e).split("'")
name_list.append(temp_list[-2])
# print ip_address
return [name_list, ip_address, ping_artt]
return e
except Timeout:
# print ip_address + ' time out'
return -3
except Exception as e:
# print ip_address + '\n' + str(e)
return -4
def get_ip(ip_s):
"""
read 3-tuplues, get host and filter to iter, it's some bugs because I don't know thread well
"""
# print ip_s
for ip_tuple in ip_s:
for nu in range(ip_tuple[1], ip_tuple[2] + 1):
ip = ip_tuple[0] + '.' + str(nu)
get_thread = GetHost(ip)
get_thread.start()
print(threading.activeCount() - init_threading_count, 'threading working...')
while threading.activeCount() > init_threading_count:
pass
def group_ip(ip_ss):
for temp_list in ip_ss:
if 'appengine' in str(temp_list[0]):
# yield {temp_list[1]: 'appengine'}
yield {'name': 'appengine', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'appspot' in str(temp_list[0]):
yield {'name': 'appspot', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'android.com' in (temp_list[0]):
yield {'name': 'android', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'ggpht' in str(temp_list[0]):
yield {'name': 'ggpht', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'gstatic.com' in str(temp_list[0]):
yield {'name': 'gstatic', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'googleapis' in str(temp_list[0]):
yield {'name': 'googleapis', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'talk' in str(temp_list[0]):
yield {'name': 'talk', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'googleusercontent' in str(temp_list[0]):
yield {'name': 'googleusercontent', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'googlecode' in str(temp_list[0]):
yield {'name': 'googlecode', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'googlesource' in str(temp_list[0]):
yield {'name': 'googlesource', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'googlevideo' in str(temp_list[0]):
yield {'name': 'googlevideo', 'ip': temp_list[1], 'ping': temp_list[2]}
if 'googlegroups' in str(temp_list[0]):
yield {'name': 'googlegroups', 'ip': temp_list[1], 'ping': temp_list[2]}
if '*.google.com' in temp_list[0] or 'google.com' in temp_list[0]:
yield {'name': 'google', 'ip': temp_list[1], 'ping': temp_list[2]}
def dic_to_config(input_iter):
"""
all iter change to config file
"""
iplist = []
if pyver2:
import ConfigParser
from ConfigParser import DuplicateSectionError
config = ConfigParser.RawConfigParser()
else:
import configparser
from configparser import DuplicateSectionError
config = configparser.RawConfigParser()
for m in input_iter:
try:
config.add_section(m['name'])
except DuplicateSectionError:
pass
if p:
if m['name'] == 'google' and int(m['ping']) < p: # ping < 100
iplist.append(m['ip'])
else:
if m['name'] == 'google':
ipList.append(m['ip'])
config.set(m['name'], m['ip'], m['ping'])
config.add_section('iplist')
config.set('iplist', 'google_hk', '|'.join(iplist))
config.write(open('new_host_file', 'w'))
def net_address(net_address_s):
"""
"192.168.1.0/24" net address change to tuplues ("192.168.1", 0, 255)
"""
ipList = []
def get_ip_number_list(m, ip_number):
n = 0
temp = 2 ** m
while 1:
if m * n <= ip_number < m * (n + 1):
return [m * n, m * (n + 1)]
n += 1
for net in net_address_s:
netlist = net.split('/')
if int(netlist[1]) == 24:
yield (netlist[0][:netlist[0].rindex('.')], 0, 255)
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]):
yield ('.'.join(netlist[0].split('.')[:2] + [str(m)]), 0,255)
else:
pass # < 16 do nothing
class GetHost(threading.Thread):
def __init__(self, ip_address):
threading.Thread.__init__(self)
self.ip_address = ip_address
def run(self):
a = get_host(self.ip_address)
global lock
try:
lock.acquire()
if isinstance(a, list):
ipList.append(a)
except:
pass
finally:
lock.release()
def stop(self):
pass
def run_pro():
"""
"""
#
get_ip(net_address(net_address_set))
dic_to_config(group_ip(ipList))
# import ping
# print ping.quiet_ping('www.google.com')[2]
if __name__ == '__main__':
global lock
lock = threading.Lock()
run_pro()