-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (58 loc) · 2.05 KB
/
app.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
from flask import Flask, render_template, request, jsonify
import platform
import subprocess
import re
import ipaddress
import socket
app = Flask(__name__)
@app.route('/')
def index():
os_info = platform.system() + " " + platform.release()
return render_template('index.html', os_info=os_info)
@app.route('/scan', methods=['POST'])
def scan():
# Get the IP range input from the user
ip_input = request.form['ip_range']
ports = request.form['ports'].split(',')
# Validate CIDR format
try:
network = ipaddress.ip_network(ip_input, strict=False)
except ValueError:
return jsonify({'error': 'Invalid CIDR address format.'}), 400
devices = get_connected_devices()
scan_results = scan_ports(devices, ports)
return jsonify({'devices': scan_results})
def get_connected_devices():
devices = []
try:
output = subprocess.check_output(['arp', '-a']).decode()
pattern = re.compile(r'\((.*?)\)\s+([0-9a-fA-F:]+)')
for line in output.splitlines():
match = pattern.search(line)
if match:
ip = match.group(1)
mac = match.group(2)
devices.append({'ip': ip, 'mac': mac})
except Exception as e:
print(f"Error retrieving devices: {e}")
return devices
def scan_ports(devices, ports):
results = []
for device in devices:
ip = device['ip']
device_ports = {}
for port in ports:
port = port.strip() # Remove any extra whitespace
if port.isdigit(): # Check if the port is a number
port = int(port)
device_ports[port] = is_port_open(ip, port)
results.append({'ip': ip, 'mac': device['mac'], 'ports': device_ports})
return results
def is_port_open(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Set a timeout
result = sock.connect_ex((ip, port))
sock.close()
return result == 0 # Return True if port is open
if __name__ == '__main__':
app.run(debug=True)