wifi_qrcode.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2021/01/28 20:47:55
  6. @License : Copyright © 2017-2020 liuyuqi. All Rights Reserved.
  7. @Desc : wifi 转化为 QRcode 方便扫码连接wifi。
  8. '''
  9. import re,os,sys
  10. import qrcode
  11. import pathlib
  12. import subprocess
  13. from shutil import which
  14. def getSSID():
  15. if sys.platform == "darwin":
  16. airport = pathlib.Path(
  17. "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport")
  18. if not airport.is_file():
  19. print_error(f"Can't find 'airport' command at {airport}")
  20. ssid = run_command(
  21. f"{airport} -I | awk '/ SSID/ {{print substr($0, index($0, $2))}}'")
  22. ssid = ssid.replace("\n", "")
  23. elif sys.platform == "linux":
  24. if which("iwgetid") is not None:
  25. print_error("Can't find the 'iwgetid' command")
  26. ssid = run_command("iwgetid -r")
  27. ssid = ssid.replace("\n", "")
  28. elif sys.platform == "win32":
  29. ssid = run_command(
  30. "netsh wlan show interfaces | findstr SSID").replace("\r", "")
  31. ssid = re.findall(r"[^B]SSID\s+:\s(.*)", ssid)[0]
  32. return ssid
  33. def getPasswordWithSSID(ssid):
  34. if sys.platform == "darwin":
  35. password = run_command(
  36. f"security find-generic-password -l \"{ssid}\" -D 'AirPort network password' -w")
  37. password = password.replace("\n", "")
  38. elif sys.platform == "linux":
  39. # Check if the user is running with super user privilages
  40. if os.geteuid() != 0:
  41. print_error(f"You need to run '{sys.argv[0]}' as root")
  42. password = run_command(f"cat /etc/NetworkManager/system-connections/{ssid} | grep psk=")
  43. password = password.replace("\n", "")
  44. elif sys.platform == "win32":
  45. password = run_command(f"netsh wlan show profile name=\"{ssid}\" key=clear | findstr Key").replace("\r", "")
  46. password = re.findall(r"Key Content\s+:\s(.*)", password)[0]
  47. if password == "":
  48. print_error("Cound not find password")
  49. return password
  50. def genQRCode(text):
  51. qr = qrcode.QRCode(version=1,
  52. error_correction=qrcode.constants.ERROR_CORRECT_L,
  53. box_size=10,
  54. border=4)
  55. qr.add_data(text)
  56. if image:
  57. file_name = ssid.replace(" ", "_") + ".png"
  58. img = qr.make_image()
  59. img.save(file_name)
  60. print(f"QR code has been saved to {file_name}")
  61. else:
  62. qr.make()
  63. qr.print_tty()
  64. def run_command(command):
  65. output, _ = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True).communicate()
  66. return output.decode("utf-8")
  67. if __name__ == "__main__":
  68. ssid=getSSID()
  69. if(ssid!=""):
  70. password=getPasswordWithSSID(ssid)
  71. text = f"WIFI:T:WPA;S:{ssid};P:{password};;"
  72. genQRCode(text)
  73. else:
  74. print("Please connect to Wifi.")