经常会遇到这样的情况:客人上门问你wifi密码。尴尬的是,你忘记了你的wifi密码。但是你的其他设备已经连上了WiFi,如何使用这些设备重新获取WiFi密码呢?一种方法是登录到路由器管理页面,但如果您连路由器密码都不记得了,那就太尴尬了。另一种方法是通过iCloudkeychain,但是这种方法很麻烦,需要通过备份获取。今天教大家一个最简单的方法:通过Python找回当前使用的wifi密码。1.在开始之前,您需要确保您的计算机上已经成功安装了Python。如果没有,请访问这篇文章:超级详细的Python安装指南进行安装。如果使用Python进行数据分析,可以直接安装Anaconda:Python数据分析挖掘的好帮手——Anaconda。另外,你需要一台已经连上Wifi的电脑,macOS和windows都可以。2、原理分析获取密码本质上就是使用命令的方式,比如在Windows下获取wifi密码:netshwlanshowprofilename=Wifinamekey=clear|findstrmacOS下获取WiFi密码的关键内容:sudosecurityfind-generic-password-lwifiname-D'AirPortnetworkpassword'-wLinux下获取WiFi密码:sudocat/etc/NetworkManager/system-connections/wifiname|greppsk=通过这三个命令,可以得到当前使用的WiFi名称。3、写代码首先封装命令:deffetch_password(system,wifi_name):"""用于获取命令参数:system{str}--系统类型wifi_name{str}--wifi名称返回:str--password作者:Python实用书《》ifsystem=="linux":command=f"sudocat/etc/NetworkManager/system-connections/{wifi_name}|greppsk="elifsystem=="macos":command=f"sudosecurityfind-generic-password-l{wifi_name}-D'AirPort网络密码'-w"elifsystem=="windows":command=f"netshwlanshowprofilename={wifi_name}key=clear|findstrkeycontent"result=fetch_result(system,command)returnresult其中,fetch_result用于执行命令获取数据:deffetch_result(system,command):"""用于执行命令获取结果参数:system{str}--systemtypecommand{str}--commandReturns:str--decodedpassword作者:Python实用宝典"""结果,_=subprocess.Popen(command,stdout=subprocess.PIPE,shell=True).communicate()返回decode_result(system,result)decode_result用于解码命令:defdecode_result(system,result):"""解码密码参数:system{str}--[systemtype]result{str}--[output]返回:[str]--[解码密码]作者:Python实用宝典"""ifsystem=="windows":#cmd命令得到的结果为bytes类型,需要解码result=result.decode("gb2312")result=result.strip('\r|\n')如果result!="":result=result.replace("","")result=result[result.find(":")+1:]result=result[result.find("=")+1:]returnresult大功告成,只需要执行:print(fetch_password('systemtype','wifiname'))获取密码电脑已经连接了其他wifi,并且没有删除相关的网络配置。其实这个函数也可以用来获取其他wifi密码。以上是完整的源代码。如果懒得再敲一遍,可以访问github链接获取:https://github.com/Ckend/pyth...我们的文章到此结束。如果喜欢我们今天的Python实践,请继续关注我们的教程。如果对您有帮助,请点击下方的赞/观看。有什么问题可以在下方留言区留言,我们会耐心解答!Python实战宝典不只是合集欢迎关注公众号:Python实战宝典原文来自Python实战宝典:Python任意系统找回Wi-Fi密码
