-
Notifications
You must be signed in to change notification settings - Fork 0
/
portscanner.py
executable file
·128 lines (108 loc) · 3.07 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/python3
import sys
import socket
from datetime import datetime
print("""
____________________
< Port Scanner v.1.1.2 >
--------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\\
||----w |
|| ||
""")
if len(sys.argv) > 4:
if(sys.argv[3] == '-p'):
if(len(sys.argv)> 5):
p1 = int(sys.argv[4])
p2 = int(sys.argv[5])+1
else:
p1 = 1
p2 = 65536
def helpfun():
print("""
Usage:
./portscanner.py [options] value [optional] value
options:
-i ip address
-h hostname
-l list of ip addresses or hostnames
optional:
-p range of ports Ex: -p 80 8888 (default range 1 to 65535)
""")
sys.exit()
def elsefun():
print('Invalid amount of arguments.')
print('Use ./portscanner.py --help for more information')
print('Exiting the program...')
sys.exit()
def portscan(target):
k = 1
for port in range(p1,p2):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target,port))
if result == 0:
k = k + 1
print('Port {} is open '.format(port))
scanupdate = 'Scanning port {}'.format(port)
print (scanupdate, end='\r')
s.close()
if k == 1:
print('Last Scanned port {}'.format(p2-1))
print('No Ports open.')
def run(target):
try:
portscan(target)
except KeyboardInterrupt:
print('\n Exiting Program.')
sys.exit()
except socket.gaierror:
print('Hostname could not be resolved.')
sys.exit()
except socket.error:
print('Can not connect to server.')
sys.exit()
if(len(sys.argv) == 1 ):
helpfun()
if len(sys.argv) >= 2:
if(sys.argv[1] == '--help'):
helpfun()
if(sys.argv[1] == '-i'):
if(len(sys.argv) == 2):
ip = input('Enter ip address: ')
target = socket.gethostbyname(ip)
else:
target = socket.gethostbyname(sys.argv[2])
print('-'*50)
print('Scanning target '+target)
print('Scan Started: '+str(datetime.now()))
print('-'*50)
run(target)
if(sys.argv[1] == '-h'):
if(len(sys.argv) == 2):
host = input('Enter hostname: ')
target = socket.gethostbyname(host)
else:
target = socket.gethostbyname(sys.argv[2])
print('-'*50)
print('Scanning target '+target)
print('Scan Started: '+str(datetime.now()))
print('-'*50)
run(target)
if(sys.argv[1] == '-l'):
f = open( sys.argv[2], 'r' )
iplists = f.read().split('\n')
for target in iplists:
print('-'*50)
print('Scanning target '+target)
print('Scan Started: '+str(datetime.now()))
print('-'*50)
run(target)
else:
elsefun()
print('-'*50)
print('Scan Ended: '+str(datetime.now()))
print('-'*50)
sys.exit()