WiFi共享二维码自动生成
前言
现今WiFi的使用已经覆盖到千家万户之中,甚至由于需求在一个地方会有多个WiFi。
当亲朋好友或者同事伙伴等首次来或WiFi密码有所改变时,就会存在要连接WiFi的需求。如果WiFi密码过于简单password、66668888,会不安全也不放心;若密码过于复杂Dr0jEVQw13XzkGMT、uJp03LNN8SMZ3XLi,告知与输入都极为麻烦。
现在的智能手机已经可以实现快速共享当时已连接的WiFi,如下图:
但仍会存在各种不便,不如将生成各个WiFi共享二维码图片后打印出来,此时使用手机扫二维码即可。这里分享推荐一个GitHub项目:wifi-password,该项目支持Windows、Linux、macOS。
来源:https://github.com/sdushantha/wifi-password
基本原理
对于一般使用的WiFi,当电脑连接成功后,此时电脑内已经有了此WiFi相关的信息如名称、密码等。
此Github项目主要基于两条电脑系统指令(本文均以Windows为例,以下均不赘述)。
首先,
netsh wlan show interfaces
主要为了获取SSID,后面的值即为连接的WiFi名称。
其次,
netsh wlan show profile name="SSID" key=clear
主要为了获取关键内容,后面的值即为连接的WiFi密码。
最后根据SSID、关键内容生成二维码图片即可。
Github上的实现与使用
使用pip安装
python3 -m pip install --user wifi-password
使用git安装
git clone https://github.com/sdushantha/wifi-password
cd wifi-password
python3 setup.py install
使用AUR安装
sudo pamac build wifi-password
基于的语言:python
需要安装的库:qrcode、image、Pillow、colorama
根据需求使用USage
usage: wifi_password [options]
optional arguments:
-h, --help show this help message and exit
--show-qr, -show Show a ASCII QR code onto the terminal/console
--save-qr [PATH], -save [PATH]
Create the QR code and save it as an image
--ssid SSID, -s SSID Specify a SSID that you have previously connected to
--version Show version number
代码方法的实现与解释
会存在一些情况导致使用失败,可以通过下载源代码,通过相应修改代码的运行方式进行使用。
代码的下载
下载链接一:
https://files.pythonhosted.org/packages/source/w/wifi-password/wifi-password-1.1.1.tar.gz
下载地址二:
https://github.com/sdushantha/wifi-password/archive/master.zip
下载解压缩后:
其中wifi_password文件夹内容为:
主要使用与修改wifi_password.py源文件(以下为源代码):
#!/usr/bin/env python3
"""
Quickly fetch your WiFi password and if needed, generate a QR code
of your WiFi to allow phones to easily connect
by Siddharth Dushantha
"""
import pathlib
import sys
import subprocess
import argparse
from shutil import which
import re
import os
import qrcode
import colorama
__version__ = "1.1.1"
def run_command(command: str) -> str:
"""
Runs a given command using subprocess module
"""
env = os.environ.copy()
env["LANG"] = "C"
output, _ = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, shell=True, env=env).communicate()
return output.decode("utf-8").rstrip("\r\n")
def print_error(text) -> None