-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
95 lines (85 loc) · 2.43 KB
/
hello.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
from flask import Flask, render_template, request
import socket
import sys
app = Flask(__name__)
@app.route("/")
def index():
return render_template('form.html')
@app.route('/', methods=['POST'])
def form_data():
ip_domain = request.form['firstname']
port = request.form['lastname']
if(port == ""):
return """<p>Invalid port!</p>
<form action="/" method="get">
<input type="submit" value="Try again!" />
</form>"""
port_range = port.split("-")
try:
int(port[0])
int(port[1])
except Exception as e:
print(e)
return """<p>Invalid port!</p>
<form action="/" method="get">
<input type="submit" value="Try again!" />
</form>"""
timeout = 5
lst = check(ip_domain, port_range, timeout)
total = "The ports that are open for " + ip_domain + " are: "
if len(lst) == 0:
return """<p>No ports are open in this range.</p>
<form action="/" method="get">
<input type="submit" value="Try again!" />
</form>"""
for ports in range(len(lst)):
if(ports == len(lst)-1):
total += str(lst[ports])
else:
total += str(lst[ports])
return """<p>{total_string}</p>
<form action="/" method="get">
<input type="submit" value="Try again!" />
</form>""".format(total_string=total)
def connect_to_ip(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
return sock
except Exception:
return None
def scan_port(ip, port, timeout):
socket.setdefaulttimeout(timeout)
sock = connect_to_ip(ip, port)
if sock:
print('Able to connect to: {0}:{1}').format(ip, port)
sock.close()
return True
else:
print('Not able to connect to: {0}:{1}').format(ip, port)
return False
def check(ip_domain, port_range, timeout):
# Get the IP address if the host name is a domain
try:
ip = socket.gethostbyname(ip_domain)
except Exception as e:
print(e)
return []
ports = []
# If the user only entered one port we will only scan the one port
# otherwise scan the range
try:
if len(port_range) < 2:
if(scan_port(ip, int(port_range[0]), int(timeout))):
ports.append(port_range[0])
else:
for port in range(int(port_range[0]), int(port_range[1])+1):
if(scan_port(ip, port, int(timeout))):
ports.append(port)
except Exception as e:
print(e)
return []
print(ports)
return ports
if __name__ == "__main__":
app.run()