Skip to content

Commit a150d47

Browse files
committed
fix keyboard interrupt handling
1 parent cf134a1 commit a150d47

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

tmap

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import threading
99
try:
1010
import socks
1111
except ImportError as ie:
12-
print("Socks module not found.\nMake sure to install all requirements with\npip3 install -r requirments.txt")
12+
print("Socks module not found.\nMake sure to install all requirements with\npip3 install -r requirments.txt\nAlternatively you can install python3-socks package on debian based systems with:\nsudo apt install python3-socks")
1313
exit()
1414

15-
VERSION='1.3.1'
15+
VERSION='1.3.2'
1616

1717
def getPortInfo(port, portfile):
1818
"""
@@ -110,8 +110,6 @@ def connScan(host, port, wait, notor, openports):
110110
sckt.close()
111111
openports.append(port)
112112
return True
113-
except KeyboardInterrupt:
114-
exit()
115113
except:
116114
return False
117115

@@ -132,12 +130,20 @@ def portScan(host, ports, wait, notor, jobs):
132130
if p > 65535:
133131
return openports
134132

135-
while threading.activeCount() >= jobs + 1:
133+
while threading.activeCount() >= jobs + 1:
136134
pass
137135

138-
thread=threading.Thread(target=connScan,args=(host, p, wait, notor, openports))
139-
threads.append(thread)
140-
thread.start()
136+
try:
137+
thread=threading.Thread(target=connScan,args=(host, p, wait, notor, openports))
138+
thread.daemon=True
139+
threads.append(thread)
140+
thread.start()
141+
## need to sleep so Keybaord Interrupt is caught
142+
time.sleep(0.1)
143+
except (KeyboardInterrupt, SystemExit):
144+
print("Caught keyboard interrupt. Exiting...")
145+
## return tuple instead of just openports, so hostScan knows about interrupt
146+
return (openports,False)
141147

142148
## Wait until all threads are done
143149
for thread in threads:
@@ -164,21 +170,35 @@ def hostScan(host, ports, wait, notor, jobs):
164170
if ips.num_addresses > 1:
165171
for ip in ips.hosts():
166172
if ip.is_private:
167-
ret[str(ip)] = portScan(str(ip), ports, wait, True, jobs)
173+
resultportscan = portScan(str(ip), ports, wait, True, jobs)
168174
else:
169-
ret[str(ip)] = portScan(str(ip), ports, wait, notor, jobs)
175+
resultportscan = portScan(str(ip), ports, wait, notor, jobs)
176+
if isinstance(resultportscan, list):
177+
ret[str(ip)] = resultportscan
178+
else:
179+
ret[str(ip)] = resultportscan[0]
180+
return ret
170181
else:
171182
if ips.is_private:
172-
ret[str(host)] = portScan(str(host), ports, wait, True, jobs)
183+
resultportscan = portScan(str(host), ports, wait, True, jobs)
184+
else:
185+
resultportscan = portScan(str(host), ports, wait, notor, jobs)
186+
if isinstance(resultportscan, list):
187+
ret[str(host)] = resultportscan
173188
else:
174-
ret[str(host)] = portScan(str(host), ports, wait, notor, jobs)
189+
ret[str(host)] = resultportscan[0]
190+
return ret
191+
175192
## Otherwise scan host as usual
176193
except:
177194
if host == 'localhost':
178-
ret[str(host)] = portScan(str(host), ports, wait, True, jobs)
195+
resultportscan = portScan(str(host), ports, wait, True, jobs)
179196
else:
180-
ret[str(host)] = portScan(str(host), ports, wait, notor, jobs)
181-
197+
resultportscan = portScan(str(host), ports, wait, notor, jobs)
198+
if isinstance(resultportscan, list):
199+
ret[str(host)] = resultportscan
200+
else:
201+
ret[str(host)] = resultportscan[0]
182202
return ret
183203

184204
def parseArgs(parser):

0 commit comments

Comments
 (0)