伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

使用Python生成自己的WiFi二维码

来源:本站原创 浏览:45次 时间:2023-07-19
简介

这个Python脚本可以查看你现在连接的WiFi的密码,可以生成一个二维码,手机扫描二维码就可以共享你的WiFi密码给被人,而不用告诉别人你的WiFi密码,避免泄露WiFi密码。

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Author: Aiker Zhao# @Date  : 2020/1/28 3:20 下午# @File  : wifi.py# @Desc  :import pathlibimport sysimport subprocessimport argparsefrom shutil import whichimport reimport osimport qrcode__version__ = "1.1"def run_command(command):    output, _ = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True).communicate()    return output.decode("utf-8")def print_error(text):    print(f"ERROR: {text}")    sys.exit(1)def get_ssid():    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 ssiddef get_password(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 passworddef generate_qr_code(ssid, password, image=True):    # Source: https://git.io/JtLIv    text = f"WIFI:T:WPA;S:{ssid};P:{password};;"    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 main():    parser = argparse.ArgumentParser(usage='%(prog)s [options]')    parser.add_argument('--qrcode', "-q", action="store_true", default=False, help="Don't generate a QR code")    parser.add_argument('--image', "-i", action="store_true", default=False,                        help="Create QR code as image instead of showing it on the terminal")    parser.add_argument('--ssid', "-s", default=get_ssid(), help="Specify a SSID that you have previously connected to")    parser.add_argument('--version', action="store_true", help="Show version number")    args = parser.parse_args()    if args.version:        print(__version__)    password = get_password(args.ssid)    if args.qrcode:        args.no_password = True        generate_qr_code(args.ssid, password, image=args.image)        return    ssid = get_ssid()    print(password)    generate_qr_code(ssid, password)if __name__ == "__main__":    main()
  • requirements.txt
asgiref==3.3.1certifi==2020.12.5chardet==4.0.0coverage==5.3.1idna==2.10image==1.5.33Pillow==8.1.0pytz==2020.5pyxll==5.0.8qrcode==6.1requests==2.25.1six==1.15.0sqlparse==0.4.1
pip3 install -r requirements.txtpython3 wifi.py
  • 生成一个以SSID命名的二维码图片文件

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net