import socket,urllib2,threading,time,httplib

global working
working = []

def try_server(host, https=False):
	
	url = "http://" + host + "/invoker/JMXInvokerServlet"
	if https: url = url.replace("http://", "https://")
	try:
		data = urllib2.urlopen(url, timeout=3).read()
		if data[:4] == "\xac\xed\x00\x05":
			return True
	except (socket.error, urllib2.HTTPError, urllib2.URLError, httplib.BadStatusLine):
		pass
	return False

def try_both_server(host):
	global working
	result = try_server(host, False)
	if not result:
		result = try_server(host, True)
		if not result:
			return
	working += [host]
#	f = open("hosts", "a")
#	f.write(host+"\n")
#	f.close()
	
num_in = 0
for line in open("ips.txt"):
	num_in = num_in + 1
	line = line.strip()
	
	while threading.activeCount() >= 100:
		print(str(threading.activeCount()) + " - " + str(num_in))
		time.sleep(0.5)
	
	threading.Thread(target=try_both_server, args=[line]).start()

while threading.activeCount() != 1:
	print(str(threading.activeCount()))
	time.sleep(1)

a = open("output", "w")
for out in working:
	a.write(out+"\n")
a.close()

