-
Notifications
You must be signed in to change notification settings - Fork 0
/
portscanner.py
47 lines (41 loc) · 1.52 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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import argparse
import socket
import sys
def scan_ports(host, start_port, end_port):
""" Scan remote hosts """
#Create socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, err_msg:
print 'Socket creation failed. Error code: ' + str(err_msg[0]) + \
' Error message: ' + err_msg[1]
sys.exit()
#Get IP of remote host
try:
remote_ip = socket.gethostbyname(host)
except socket.error, error_msg:
print error_msg
sys.exit()
#Scan ports
print "Start scanning... "
end_port += 1
for port in range(start_port, end_port):
try:
ret = sock.connect_ex((remote_ip, port))
if ret == 0:
print 'Port ' + str(port) + ' is open'
sock.close()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Remote Port Scanner')
parser.add_argument('--host', action="store", dest="host", default='localhost')
parser.add_argument('--start-port', action="store", dest="start_port", default=1, type=int)
parser.add_argument('--end-port', action="store", dest="end_port", default=100, type=int)
#parse arguments
given_args = parser.parse_args()
host, start_port, end_port = given_args.host, given_args.start_port, given_args.end_port
scan_ports(host, start_port, end_port)