Skip to content

Commit ee7e9c5

Browse files
committed
added threading to make it faster
1 parent 6987a79 commit ee7e9c5

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

sherlock.py

+28-22
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import re
55
import paramiko, socket
66
import optparse
7+
from threading import Thread
78

89
desc = "Quick automated tool developed to find your Linux machine using your credentials. For finding machines on the same subnet, just run sherlock without any arguments. For finding machines on another subnet of the same network, use the '-e' for options."
910

11+
found = None
12+
1013
def parseCmd():
1114
p = optparse.OptionParser(description=desc)
1215
p.add_option('-e', '--ext', dest='ext', default='', help='take external subnet ip if required')
@@ -49,30 +52,33 @@ def getInput(opts):
4952
ip = patt.findall(f)
5053
return [ip, username, password]
5154

55+
def worker(host, username, password):
56+
global found
57+
try:
58+
ssh = paramiko.SSHClient()
59+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
60+
print "Trying for " + host + " ...."
61+
ssh.connect(host, username = username, password = password)
62+
print "Connected for " + host
63+
found = host
64+
return True
65+
except (paramiko.ssh_exception.BadHostKeyException, paramiko.ssh_exception.AuthenticationException,
66+
paramiko.ssh_exception.SSHException, socket.error) as e:
67+
print e
68+
return False
69+
5270
def findMc(ip, username, password):
53-
i = 1
54-
while i < len(ip):
55-
ip_addr = ip[i]
56-
try:
57-
ssh = paramiko.SSHClient()
58-
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
59-
print "Trying for " + ip_addr + '....'
60-
ssh.connect(ip_addr, username=username, password=password)
61-
print "Connected."
62-
break
63-
except (paramiko.ssh_exception.BadHostKeyException, paramiko.ssh_exception.AuthenticationException,
64-
paramiko.ssh_exception.SSHException, socket.error) as e:
65-
print e
66-
i+=1
67-
continue
68-
print ip_addr + " done"
69-
if i >= len(ip):
70-
print "Desired machine is not found"
71-
return ""
72-
else:
73-
print "Your desired IP is " + ip[i]
74-
return ip[i]
71+
threads = []
72+
for host in ip:
73+
t = Thread(target=worker, args = (host, username, password))
74+
t.start()
75+
threads.append(t)
76+
77+
for t in threads:
78+
t.join()
7579

80+
if found is not None:
81+
print "The correct destination is " + found
7682

7783
if __name__ == '__main__':
7884
opts = parseCmd()

0 commit comments

Comments
 (0)