-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwlan_manager.py
38 lines (31 loc) · 1.35 KB
/
wlan_manager.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
from flask import Flask, jsonify, request
import subprocess
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/api/wifi-networks')
def get_wifi_networks():
try:
scan_result = subprocess.check_output(['nmcli', '-t', '-f', 'SSID,SECURITY,BARS,FREQ', 'dev', 'wifi']).decode('utf-8')
networks = []
for line in scan_result.strip().split('\n'):
ssid, security, strength, freq = line.split(':')
# Entfernen Sie 'MHz' aus dem Frequenz-String und wandeln Sie ihn in einen Integer um
freq = int(freq.replace(' MHz', ''))
band = '5 GHz' if freq >= 5000 else '2.4 GHz'
networks.append({'ssid': ssid, 'security': security, 'strength': strength, 'band': band})
return jsonify(networks)
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/api/connect', methods=['POST'])
def connect_to_network():
data = request.json
ssid = data['ssid']
password = data['password']
try:
subprocess.run(['nmcli', 'dev', 'wifi', 'connect', ssid, 'password', password], check=True)
return jsonify({'result': 'Verbindung erfolgreich hergestellt'})
except subprocess.CalledProcessError as e:
return jsonify({'result': f'Fehler bei der Verbindung. Falsches Password?'}), 500
if __name__ == '__main__':
app.run(port=5000)