Browse Source

增加 Python 方式实现。

liuyuqi-dellpc 3 years ago
parent
commit
4d8201c843
4 changed files with 114 additions and 0 deletions
  1. 5 0
      Python/Dockerfile
  2. 1 0
      Python/requirements.txt
  3. 92 0
      Python/wifi_qrcode.py
  4. 16 0
      README.md

+ 5 - 0
Python/Dockerfile

@@ -0,0 +1,5 @@
+FROM python:3.6 as builder
+WORKDIR /app
+ADD . /app
+RUN pip install -r requirements.txt
+CMD [ "python","-m","wifi_qecode.py" ]

+ 1 - 0
Python/requirements.txt

@@ -0,0 +1 @@
+qrcode

+ 92 - 0
Python/wifi_qrcode.py

@@ -0,0 +1,92 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+'''
+@Contact :   liuyuqi.gov@msn.cn
+@Time    :   2021/01/28 20:47:55
+@License :   Copyright © 2017-2020 liuyuqi. All Rights Reserved.
+@Desc    :   wifi 转化为 QRcode 方便扫码连接wifi。
+'''
+
+import re,os,sys
+import qrcode
+
+import pathlib
+import subprocess
+from shutil import which
+
+def getSSID():
+    if sys.platform == "darwin":
+        airport = pathlib.Path(
+            "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport")
+        if not airport.is_file():
+            print_error(f"Can't find 'airport' command at {airport}")
+
+        ssid = run_command(
+            f"{airport} -I | awk '/ SSID/ {{print substr($0, index($0, $2))}}'")
+        ssid = ssid.replace("\n", "")
+
+    elif sys.platform == "linux":
+        if which("iwgetid") is not None:
+            print_error("Can't find the 'iwgetid' command")
+
+        ssid = run_command("iwgetid -r")
+        ssid = ssid.replace("\n", "")
+
+    elif sys.platform == "win32":
+        ssid = run_command(
+            "netsh wlan show interfaces | findstr SSID").replace("\r", "")
+        ssid = re.findall(r"[^B]SSID\s+:\s(.*)", ssid)[0]
+    return ssid
+
+def getPasswordWithSSID(ssid):
+    if sys.platform == "darwin":
+        password = run_command(
+            f"security find-generic-password -l \"{ssid}\" -D 'AirPort network password' -w")
+        password = password.replace("\n", "")
+
+    elif sys.platform == "linux":
+        # Check if the user is running with super user privilages
+        if os.geteuid() != 0:
+            print_error(f"You need to run '{sys.argv[0]}' as root")
+
+        password = run_command(f"cat /etc/NetworkManager/system-connections/{ssid} | grep psk=")
+        password = password.replace("\n", "")
+
+    elif sys.platform == "win32":
+        password = run_command(f"netsh wlan show profile name=\"{ssid}\" key=clear | findstr Key").replace("\r", "")
+        password = re.findall(r"Key Content\s+:\s(.*)", password)[0]
+
+    if password == "":
+        print_error("Cound not find password")
+
+    return password
+
+def genQRCode(text):
+    qr = qrcode.QRCode(version=1,
+                       error_correction=qrcode.constants.ERROR_CORRECT_L,
+                       box_size=10,
+                       border=4)
+    qr.add_data(text)
+
+    if image:
+        file_name = ssid.replace(" ", "_") + ".png"
+        img = qr.make_image()
+        img.save(file_name)
+        print(f"QR code has been saved to {file_name}")
+    else:
+        qr.make()
+        qr.print_tty()
+
+
+def run_command(command):
+    output, _ = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True).communicate()
+    return output.decode("utf-8")
+
+if __name__ == "__main__":
+    ssid=getSSID()
+    if(ssid!=""):
+        password=getPasswordWithSSID(ssid)
+        text = f"WIFI:T:WPA;S:{ssid};P:{password};;"
+        genQRCode(text)
+    else:
+        print("Please connect to Wifi.")

+ 16 - 0
README.md

@@ -0,0 +1,16 @@
+# WifiQRCode
+
+
+wifi share with QRCode.
+
+# Develop
+
+## Python
+
+```
+virtualenv -p /opt/python/3.7.4/bin/python3 venv
+source venv/bin/activate
+pip install -r requirements.txt
+
+python wifi_qrcode.py
+```