forked from ahervias77/portscanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portscanner.py
66 lines (45 loc) · 1.89 KB
/
portscanner.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
#!/usr/bin/python3
import socket
import sys
def scanHost(ip, startPort, endPort):
""" Starts a TCP scan on a given IP address """
print('[*] Starting TCP port scan on host %s' % ip)
# Begin TCP scan on host
tcp_scan(ip, startPort, endPort)
print('[+] TCP scan on host %s complete' % ip)
def scanRange(network, startPort, endPort):
""" Starts a TCP scan on a given IP address range """
print('[*] Starting TCP port scan on network %s.0' % network)
# Iterate over a range of host IP addresses and scan each target
for host in range(1, 255):
ip = network + '.' + str(host)
tcp_scan(ip, startPort, endPort)
print('[+] TCP scan on network %s.0 complete' % network)
def tcp_scan(ip, startPort, endPort):
""" Creates a TCP socket and attempts to connect via supplied ports """
for port in range(startPort, endPort + 1):
try:
# Create a new socket
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Print if the port is open
if not tcp.connect_ex((ip, port)):
print('[+] %s:%d/TCP Open' % (ip, port))
tcp.close()
except Exception:
pass
if __name__ == '__main__':
# Timeout in seconds
socket.setdefaulttimeout(0.01)
if len(sys.argv) < 4:
print('Usage: ./portscanner.py <IP address> <start port> <end port>')
print('Example: ./portscanner.py 192.168.1.10 1 65535\n')
print('Usage: ./portscanner.py <network> <start port> <end port> -n')
print('Example: ./portscanner.py 192.168.1 1 65535 -n')
elif len(sys.argv) >= 4:
network = sys.argv[1]
startPort = int(sys.argv[2])
endPort = int(sys.argv[3])
if len(sys.argv) == 4:
scanHost(network, startPort, endPort)
if len(sys.argv) == 5:
scanRange(network, startPort, endPort)