|
@@ -3,23 +3,26 @@
|
|
|
'''
|
|
|
@Contact : liuyuqi.gov@msn.cn
|
|
|
@Time : 2021/01/28 20:47:55
|
|
|
-@License : Copyright © 2017-2020 liuyuqi. All Rights Reserved.
|
|
|
+@License : Copyright © 2021 liuyuqi.gov@msn.cn. All Rights Reserved. Licensed under the Apache 2.0 license.
|
|
|
@Desc : wifi 转化为 QRcode 方便扫码连接wifi。
|
|
|
'''
|
|
|
|
|
|
import re,os,sys
|
|
|
import qrcode
|
|
|
-
|
|
|
+import logging
|
|
|
import pathlib
|
|
|
import subprocess
|
|
|
from shutil import which
|
|
|
|
|
|
+# 是否输出图片
|
|
|
+image=True
|
|
|
+
|
|
|
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}")
|
|
|
+ logging.error(f"Can't find 'airport' command at {airport}")
|
|
|
|
|
|
ssid = run_command(
|
|
|
f"{airport} -I | awk '/ SSID/ {{print substr($0, index($0, $2))}}'")
|
|
@@ -27,7 +30,7 @@ def getSSID():
|
|
|
|
|
|
elif sys.platform == "linux":
|
|
|
if which("iwgetid") is not None:
|
|
|
- print_error("Can't find the 'iwgetid' command")
|
|
|
+ logging.error("Can't find the 'iwgetid' command")
|
|
|
|
|
|
ssid = run_command("iwgetid -r")
|
|
|
ssid = ssid.replace("\n", "")
|
|
@@ -47,7 +50,7 @@ def getPasswordWithSSID(ssid):
|
|
|
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")
|
|
|
+ logging.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", "")
|
|
@@ -57,7 +60,7 @@ def getPasswordWithSSID(ssid):
|
|
|
password = re.findall(r"Key Content\s+:\s(.*)", password)[0]
|
|
|
|
|
|
if password == "":
|
|
|
- print_error("Cound not find password")
|
|
|
+ logging.error("Cound not find password")
|
|
|
|
|
|
return password
|
|
|
|
|
@@ -72,7 +75,7 @@ def genQRCode(text):
|
|
|
file_name = ssid.replace(" ", "_") + ".png"
|
|
|
img = qr.make_image()
|
|
|
img.save(file_name)
|
|
|
- print(f"QR code has been saved to {file_name}")
|
|
|
+ logging.info(f"QR code has been saved to {file_name}")
|
|
|
else:
|
|
|
qr.make()
|
|
|
qr.print_tty()
|
|
@@ -83,10 +86,10 @@ def run_command(command):
|
|
|
return output.decode("utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
- ssid=getSSID()
|
|
|
- if(ssid!=""):
|
|
|
- password=getPasswordWithSSID(ssid)
|
|
|
+ ssid = getSSID()
|
|
|
+ if(ssid != ""):
|
|
|
+ password = getPasswordWithSSID(ssid)
|
|
|
text = f"WIFI:T:WPA;S:{ssid};P:{password};;"
|
|
|
genQRCode(text)
|
|
|
else:
|
|
|
- print("Please connect to Wifi.")
|
|
|
+ logging.info("Please connect to Wifi.")
|