wifi_qrcode.py 3.0 KB

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