Python中如何快速获取WiFi密码和公网IP


Python中如何快速获取WiFi密码和公网IP
29 回复

wifi 密码无线属性、安全就能看
公网 ip 用这个就行 http://www.net.cn/static/customercare/yourip.asp 。。。


import subprocess
import re
import requests

def get_wifi_passwords():
    """获取Windows系统保存的WiFi密码"""
    try:
        # 执行netsh命令获取所有WiFi配置
        result = subprocess.run(
            ["netsh", "wlan", "show", "profiles"],
            capture_output=True,
            text=True,
            encoding='gbk'
        )
        
        # 提取所有WiFi名称
        profiles = re.findall(r"所有用户配置文件 : (.*)", result.stdout)
        
        passwords = {}
        for profile in profiles:
            profile = profile.strip()
            # 获取每个WiFi的详细配置
            cmd = f'netsh wlan show profile name="{profile}" key=clear'
            result = subprocess.run(
                cmd,
                shell=True,
                capture_output=True,
                text=True,
                encoding='gbk'
            )
            
            # 查找密码
            key_match = re.search(r"关键内容            : (.*)", result.stdout)
            if key_match:
                passwords[profile] = key_match.group(1).strip()
            else:
                passwords[profile] = "无密码或密码不可见"
        
        return passwords
        
    except Exception as e:
        return f"获取WiFi密码失败: {e}"

def get_public_ip():
    """获取公网IP地址"""
    try:
        # 使用多个服务提供商,增加可靠性
        services = [
            'https://api.ipify.org',
            'https://ident.me',
            'https://checkip.amazonaws.com'
        ]
        
        for service in services:
            try:
                response = requests.get(service, timeout=5)
                if response.status_code == 200:
                    return response.text.strip()
            except:
                continue
        
        return "无法获取公网IP"
        
    except Exception as e:
        return f"获取公网IP失败: {e}"

if __name__ == "__main__":
    print("正在获取WiFi密码...")
    wifi_passwords = get_wifi_passwords()
    
    if isinstance(wifi_passwords, dict):
        print("\n已保存的WiFi密码:")
        for wifi, password in wifi_passwords.items():
            print(f"  {wifi}: {password}")
    else:
        print(wifi_passwords)
    
    print("\n正在获取公网IP...")
    public_ip = get_public_ip()
    print(f"公网IP地址: {public_ip}")

这段代码做了两件事:

  1. 获取WiFi密码:通过Windows的netsh命令读取系统保存的WiFi配置信息,正则匹配出密码。注意这只能获取当前系统已连接过且保存了密码的WiFi。

  2. 获取公网IP:通过访问多个公共IP查询服务,哪个能用用哪个,这样更可靠。

注意:获取WiFi密码的功能只在Windows系统有效,且需要管理员权限。Linux/macOS系统需要用不同的方法。

一句话建议:记得处理权限和跨平台兼容性问题。

这不是用 shell 命令就行了吗,比如 mac 下:curl myip.ipip.net && security find-generic-password -wa wifi-name

ip111.cn 能看出国 ip 和代理 ip

里面用到 nm-tool,在 ubuntu 16.04 已经被替换为 nmtui

ng 和 angular-cli 重名了

查了一下,15.04 就已经不支持,多谢了

老哥会玩。。

原理一样

获取公网 ip 我用 python -c “import socket; sock=socket.create_connection((‘ns1.dnspod.net’,6666)); print sock.recv(16); sock.close()”

获取公网 ip 我都是从百度直接搜 ip

方法都差不多

macOS 看起来都是一行呢

security find-generic-password -wa $(networksetup -getairportnetwork en0 | awk ‘{print $NF}’ )



可以

$ python -c 's=import(“socket”).socket(2,2);s.connect((“8.8.8.8”, 8));print(s.getsockname()[0])'
172.x.x.x

根本不用发起连接。

厉害,不过最好与网卡名无关,en0, 也可能是 en1, en2 等.

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep ’ SSID’

#16 这个获取的是内网 IP 啊

获取公网 IP 还是必须得发起链接吧

呃,这个是 dnspod 的特别服务么?

原来 dnspod 还能这样玩

$ nc ns1.dnspod.net 6666


不错。

➜ ~ curl ipinfo.io

还能看地理位置啥的(没什么用…

需要翻墙, 返回的都是代理 IP 的信息

查看公网 IP 一行 curl ip.cn 不就结束了么……为何要用 Python WebSocket?

另外无脑安利 https://ip.sb

可能代理策略不对? 南京联通表示能用

Windows 平台
netsh wlan show profile
netsh wlan show profile WiFi-name key=clear
不要太好用。

windows 与语言有关,解析 command 结果,稍微繁琐一点,所以暂时没做对 windows 的支持;
ps: 准备 windows 平台就简单支持中英文

哈哈, 其实是我网络比较卡,一直等待,加了代理, 直接就返回结果了。

回到顶部