From 034fa0d1cbd71dbf6f5c748138f0923caa6022a6 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 15 Mar 2019 11:56:33 +0100 Subject: [PATCH] Init push --- .gitignore | 3 +++ Core.py | 9 +++++++++ Setup.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 Core.py create mode 100644 Setup.py diff --git a/.gitignore b/.gitignore index 894a44c..1890bfd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ __pycache__/ *.py[cod] *$py.class +.ftpconfig +.ftpignore + # C extensions *.so diff --git a/Core.py b/Core.py new file mode 100644 index 0000000..3d1afa6 --- /dev/null +++ b/Core.py @@ -0,0 +1,9 @@ +import shelve +from os.path import isfile + +class Main: + def __init__(self): + if not isfile("Settings.conf"): + setup() + else: + isconnection() diff --git a/Setup.py b/Setup.py new file mode 100644 index 0000000..d6854eb --- /dev/null +++ b/Setup.py @@ -0,0 +1,41 @@ +import io, socket, subprocess +from requests import post, get +from flask import request, Flask + +#http://192.168.43.155:5000/setup?ssid=cimaphone&password=cimakodu30&id=1 + +app = Flask(__name__) + +def writeconfig(ssid, password): + rtline = "\n" + with io.open("/etc/wpa_supplicant/wpa_supplicant.conf", "w", encoding="utf8") as note: + conf = "ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\ncountry=FR\nnetwork={\nssid=\"" + conf += ssid + conf += "\"\nscan_ssid=1\npsk=\"" + conf += password + conf += "\"\n}" + note.write(conf) + subprocess.check_call(["sudo", "wpa_cli", "-i", "wlan0", "reconfigure"]) + + +def testinternet(): + result = True + try: + socket.gethostbyname("www.google.com") + except: + result = False + return result + +@app.route("/setup") +def web_setup(): + if not (request.args.get("ssid") and request.args.get("password") and request.args.get("id")): + return "Error" + else: + writeconfig(request.args.get("ssid"), request.args.get("password")) + if testinternet(): + r = post("http://flifloo.ddns.net:5000/locksetup", data = {"mac": os.open("/sys/class/net/wlan0/address").read(), "id": request.args.get("id")}) + else: + return "Cant connect" + +if __name__ == "__main__": + app.run(debug=True, port=5000)