{"id":5970,"date":"2025-07-23T19:18:29","date_gmt":"2025-07-23T11:18:29","guid":{"rendered":"http:\/\/192.168.1.29\/?p=5970"},"modified":"2025-07-23T19:48:50","modified_gmt":"2025-07-23T11:48:50","slug":"python%e5%9c%a8windows%e7%94%b5%e8%84%91%e6%89%be%e5%9b%9ewifi%e5%af%86%e7%a0%81","status":"publish","type":"post","link":"http:\/\/xc.ipyingshe.net:5347\/?p=5970","title":{"rendered":"python\u5728windows\u7535\u8111\u627e\u56deWiFi\u5bc6\u7801"},"content":{"rendered":"\n<p><strong>\u4e0d\u662f\u7834\u89e3\u5bc6\u7801<\/strong>\uff0c\u5728\u80fd\u65e0\u7ebf\u4e0a\u7f51\u7684\u60c5\u51b5\u5b50\u4e0b\uff0c\u627e\u51fa\u4fdd\u5b58\u8fc7\u7684\u5bc6\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\n\n# \u6267\u884cnetsh\u547d\u4ee4\u83b7\u53d6Wi-Fi\u5bc6\u7801\nresult = subprocess.check_output('netsh wlan show profiles', shell=True)\n# Convert bytes to string using the default encoding\nresult = result.decode()\nprint(result)\nprofiles = &#91;]\nfor line in result.split('\\n'):\n    if \"\u6240\u6709\u7528\u6237\u914d\u7f6e\u6587\u4ef6 :\" in line:\n        profiles.append(line.split(':')&#91;1].strip())\nfor profile in profiles:\n    try:\n        password_result = subprocess.check_output(f'netsh wlan show profile name=\"{profile}\" key=clear',\n                                                  shell=True)\n        # Convert bytes to string using the default encoding\n        password_result = password_result.decode()\n        # print(password_result)\n        for line in password_result.split('\\n'):\n            if \"\u5173\u952e\u5185\u5bb9\" in line:\n                print('{:&lt;40}{:>30}'.format('WIFI\u540d\u79f0\uff1a'+profile,'WIFI\u5bc6\u7801\uff1a'+line.split(':')&#91;1].strip()))\n\n    except:\n        print(f'\u65e0\u6cd5\u83b7\u53d6{profile}\u7684\u5bc6\u7801')<\/code><\/pre>\n\n\n\n<p><strong>\u66f4\u591a\u89e3\u91ca\u7684\u4ee3\u7801\uff1a<\/strong><br>import subprocess<br>import re<br>import ctypes<br>import sys<br><br>def is_admin():<br>    try:<br>        return ctypes.windll.shell32.IsUserAnAdmin()<br>    except:<br>        return False<br><br>def get_current_wifi_name():<br>    try:<br>        # \u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u89e3\u7801<br>        output = subprocess.check_output(&#8216;netsh wlan show interface&#8217;, shell=True).decode(sys.getdefaultencoding(), errors=&#8217;replace&#8217;)<br>        # \u67e5\u627e\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi\u540d\u79f0<br>        match = re.search(r&#8221;SSID\\s*:\\s*(.*)&#8221;, output)<br>        if match:<br>            return match.group(1).strip()<br>        else:<br>            print(&#8220;\u65e0\u6cd5\u83b7\u53d6\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi\u540d\u79f0&#8221;)<br>            return None<br>    except Exception as e:<br>        print(f&#8221;\u53d1\u751f\u9519\u8bef: {e}&#8221;)<br>        return None<br><br>def get_wifi_password(ssid):<br>    if not ssid:<br>        return None<br>    <br>    try:<br>        # \u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u89e3\u7801<br>        output = subprocess.check_output(f&#8217;netsh wlan show profile name=&#8221;{ssid}&#8221; key=clear&#8217;, shell=True).decode(sys.getdefaultencoding(), errors=&#8217;replace&#8217;)<br>        print(&#8220;netsh \u547d\u4ee4\u5b8c\u6574\u8f93\u51fa:&#8221;)<br>        print(output)  # \u6253\u5370\u5b8c\u6574\u8f93\u51fa<br>        # \u67e5\u627eWi-Fi\u5bc6\u7801\uff0c\u4f7f\u7528\u66f4\u7075\u6d3b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<br>        match = re.search(r&#8221;\u5173\u952e\u5185\u5bb9\\s*:\\s*(.*)&#8221;, output)<br>        if match:<br>            return match.group(1).strip()<br>        else:<br>            print(f&#8221;\u672a\u627e\u5230Wi-Fi &#8216;{ssid}&#8217;\u7684\u5bc6\u7801\uff0c\u53ef\u80fd\u8be5\u7f51\u7edc\u6ca1\u6709\u4fdd\u5b58\u5bc6\u7801\u6216\u4e3a\u5f00\u653e\u7f51\u7edc\u3002&#8221;)<br>            return None<br>    except subprocess.CalledProcessError as e:<br>        print(f&#8221;\u6267\u884c\u547d\u4ee4\u65f6\u51fa\u9519: {e.output.decode(sys.getdefaultencoding(), errors=&#8217;replace&#8217;)}&#8221;)<br>        return None<br>    except Exception as e:<br>        print(f&#8221;\u53d1\u751f\u9519\u8bef: {e}&#8221;)<br>        return None<br><br>if __name__ == &#8220;__main__&#8221;:<br>    if not is_admin():<br>        ctypes.windll.shell32.ShellExecuteW(None, &#8220;runas&#8221;, sys.executable, &#8221; &#8220;.join(sys.argv), None, 1)<br>    else:<br>        current_wifi = get_current_wifi_name()<br>        if current_wifi:<br>            print(f&#8221;\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi: {current_wifi}&#8221;)<br>            password = get_wifi_password(current_wifi)<br>            if password:<br>                print(f&#8221;Wi-Fi\u5bc6\u7801: {password}&#8221;)<br>            else:<br>                print(&#8220;\u672a\u80fd\u83b7\u53d6\u5bc6\u7801\u3002\u8bf7\u786e\u4fdd\u8be5\u7f51\u7edc\u4fdd\u5b58\u4e86\u5bc6\u7801\u3002&#8221;)<br>        else:<br>            print(&#8220;\u65e0\u6cd5\u786e\u5b9a\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi\u3002&#8221;)<br>    <br>    # \u7b49\u5f85\u7528\u6237\u8f93\u5165\u540e\u518d\u5173\u95ed\u7a97\u53e3<br>    input(&#8220;\u6309\u56de\u8f66\u952e\u9000\u51fa&#8230;&#8221;)<\/p>\n\n\n\n<p><strong>\u6709\u8be6\u7ec6\u89e3\u91ca\u7684\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import subprocess\nimport re\nimport ctypes\nimport sys\n\ndef is_admin():\n    \"\"\"\n    \u68c0\u67e5\u5f53\u524d Python \u811a\u672c\u662f\u5426\u4ee5\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884c\u3002\n\n    \u8fd4\u56de:\n        bool: \u5982\u679c\u4ee5\u7ba1\u7406\u5458\u6743\u9650\u8fd0\u884c\u8fd4\u56de True\uff0c\u5426\u5219\u8fd4\u56de False\u3002\n    \"\"\"\n    try:\n        return ctypes.windll.shell32.IsUserAnAdmin()\n    except (AttributeError, OSError):\n        # \u5904\u7406\u8bbf\u95ee\u52a8\u6001\u94fe\u63a5\u5e93\u51fd\u6570\u5931\u8d25\u6216\u7cfb\u7edf\u9519\u8bef\n        return False\n\ndef get_current_wifi_name():\n    # \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a get_current_wifi_name \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002\n    try:\n        # \u4f7f\u7528 try \u8bed\u53e5\u5757\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\n        # \u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u89e3\u7801\n        output = subprocess.check_output('netsh wlan show interface', shell=True).decode(sys.getdefaultencoding(), errors='replace')\n        # subprocess.check_output \u7528\u4e8e\u5728 Python \u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\u3002\n        # 'netsh wlan show interface' \u662f Windows \u7cfb\u7edf\u7684\u547d\u4ee4\uff0c\u7528\u4e8e\u663e\u793a\u5f53\u524d Wi-Fi \u63a5\u53e3\u7684\u4fe1\u606f\u3002\n        # shell=True \u8868\u793a\u901a\u8fc7\u7cfb\u7edf\u7684 shell \u6765\u6267\u884c\u547d\u4ee4\u3002\n        # decode(sys.getdefaultencoding(), errors='replace') \u5c06\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u7684\u5b57\u8282\u6d41\u6309\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\uff0c\n        # errors='replace' \u8868\u793a\u9047\u5230\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u7b26\u65f6\u7528\u66ff\u6362\u5b57\u7b26\u66ff\u4ee3\u3002\n        \n        # \u67e5\u627e\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi\u540d\u79f0\n        match = re.search(r\"SSID\\s*:\\s*(.*)\", output)\n        # re.search \u662f Python \u4e2d re \u6a21\u5757\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\n        # r\"SSID\\s*:\\s*(.*)\" \u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u542b\u4e49\u5982\u4e0b\uff1a\n        # \"SSID\" \u5339\u914d\u5b57\u9762\u5b57\u7b26\u4e32 \"SSID\"\uff1b\n        # \\s* \u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\uff1b\n        # \":\" \u5339\u914d\u5192\u53f7\uff1b\n        # \\s* \u518d\u6b21\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\uff1b\n        # (.*) \u662f\u4e00\u4e2a\u6355\u83b7\u7ec4\uff0c\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff08\u9664\u6362\u884c\u7b26\u5916\uff09\u96f6\u6b21\u6216\u591a\u6b21\uff0c\u7528\u4e8e\u6355\u83b7 Wi-Fi \u540d\u79f0\u3002\n        \n        if match:\n            return match.group(1).strip()\n            # \u5982\u679c\u5339\u914d\u6210\u529f\uff0cmatch.group(1) \u83b7\u53d6\u6355\u83b7\u7ec4\u4e2d\u7684\u5185\u5bb9\uff0c\u5373 Wi-Fi \u540d\u79f0\u3002\n            # strip() \u65b9\u6cd5\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u540e\u8fd4\u56de\u3002\n        else:\n            print(\"\u65e0\u6cd5\u83b7\u53d6\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi\u540d\u79f0\")\n            return None\n            # \u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u6253\u5370\u63d0\u793a\u4fe1\u606f\u5e76\u8fd4\u56de None\u3002\n    except Exception as e:\n        print(f\"\u53d1\u751f\u9519\u8bef: {e}\")\n        return None\n        # \u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u8fd4\u56de None\u3002\n\ndef get_wifi_password(ssid):\n    # \u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a get_wifi_password \u7684\u51fd\u6570\uff0c\u63a5\u6536\u4e00\u4e2a\u53c2\u6570 ssid\uff0c\u4ee3\u8868 Wi-Fi \u7f51\u7edc\u7684\u540d\u79f0\u3002\n    if not ssid:\n        # \u68c0\u67e5\u4f20\u5165\u7684 ssid \u662f\u5426\u4e3a\u7a7a\uff08\u5373 None\u3001\u7a7a\u5b57\u7b26\u4e32\u7b49\u5047\u503c\uff09\u3002\n        return None\n        # \u5982\u679c ssid \u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\u3002\n    \n    try:\n        # \u4f7f\u7528 try \u8bed\u53e5\u5757\u6765\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\n        # \u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u89e3\u7801\n        output = subprocess.check_output(f'netsh wlan show profile name=\"{ssid}\" key=clear', shell=True).decode(sys.getdefaultencoding(), errors='replace')\n        # subprocess.check_output \u7528\u4e8e\u5728 Python \u4e2d\u6267\u884c\u5916\u90e8\u547d\u4ee4\u3002\n        # f'netsh wlan show profile name=\"{ssid}\" key=clear' \u662f Windows \u7cfb\u7edf\u7684\u547d\u4ee4\uff0c\u7528\u4e8e\u663e\u793a\u6307\u5b9a Wi-Fi \u7f51\u7edc\u7684\u8be6\u7ec6\u914d\u7f6e\u4fe1\u606f\uff0c\u5305\u62ec\u5bc6\u7801\uff08key=clear \u8868\u793a\u663e\u793a\u660e\u6587\u5bc6\u7801\uff09\u3002\n        # shell=True \u8868\u793a\u901a\u8fc7\u7cfb\u7edf\u7684 shell \u6765\u6267\u884c\u547d\u4ee4\u3002\n        # decode(sys.getdefaultencoding(), errors='replace') \u5c06\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u7684\u5b57\u8282\u6d41\u6309\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\uff0c\n        # errors='replace' \u8868\u793a\u9047\u5230\u65e0\u6cd5\u89e3\u7801\u7684\u5b57\u7b26\u65f6\u7528\u66ff\u6362\u5b57\u7b26\u66ff\u4ee3\u3002\n        print(\"netsh \u547d\u4ee4\u5b8c\u6574\u8f93\u51fa:\")\n        print(output)  # \u6253\u5370\u5b8c\u6574\u8f93\u51fa\n        # \u67e5\u627eWi-Fi\u5bc6\u7801\uff0c\u4f7f\u7528\u66f4\u7075\u6d3b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\n        match = re.search(r\"\u5173\u952e\u5185\u5bb9\\s*:\\s*(.*)\", output)\n        # re.search \u662f Python \u4e2d re \u6a21\u5757\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u641c\u7d22\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u3002\n        # r\"\u5173\u952e\u5185\u5bb9\\s*:\\s*(.*)\" \u662f\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u542b\u4e49\u5982\u4e0b\uff1a\n        # \"\u5173\u952e\u5185\u5bb9\" \u5339\u914d\u4e2d\u6587\u7684 \"\u5173\u952e\u5185\u5bb9\" \u5b57\u7b26\u4e32\uff1b\n        # \\s* \u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\uff1b\n        # \":\" \u5339\u914d\u5192\u53f7\uff1b\n        # \\s* \u518d\u6b21\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\uff1b\n        # (.*) \u662f\u4e00\u4e2a\u6355\u83b7\u7ec4\uff0c\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff08\u9664\u6362\u884c\u7b26\u5916\uff09\u96f6\u6b21\u6216\u591a\u6b21\uff0c\u7528\u4e8e\u6355\u83b7 Wi-Fi \u5bc6\u7801\u3002\n        if match:\n            return match.group(1).strip()\n            # \u5982\u679c\u5339\u914d\u6210\u529f\uff0cmatch.group(1) \u83b7\u53d6\u6355\u83b7\u7ec4\u4e2d\u7684\u5185\u5bb9\uff0c\u5373 Wi-Fi \u5bc6\u7801\u3002\n            # strip() \u65b9\u6cd5\u53bb\u9664\u5b57\u7b26\u4e32\u4e24\u7aef\u7684\u7a7a\u767d\u5b57\u7b26\u540e\u8fd4\u56de\u3002\n        else:\n            print(f\"\u672a\u627e\u5230Wi-Fi '{ssid}'\u7684\u5bc6\u7801\uff0c\u53ef\u80fd\u8be5\u7f51\u7edc\u6ca1\u6709\u4fdd\u5b58\u5bc6\u7801\u6216\u4e3a\u5f00\u653e\u7f51\u7edc\u3002\")\n            return None\n            # \u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u6253\u5370\u63d0\u793a\u4fe1\u606f\u5e76\u8fd4\u56de None\u3002\n    except subprocess.CalledProcessError as e:\n        print(f\"\u6267\u884c\u547d\u4ee4\u65f6\u51fa\u9519: {e.output.decode(sys.getdefaultencoding(), errors='replace')}\")\n        return None\n        # \u6355\u83b7 subprocess.check_output \u6267\u884c\u547d\u4ee4\u5931\u8d25\u65f6\u629b\u51fa\u7684 CalledProcessError \u5f02\u5e38\uff0c\n        # \u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u8fd4\u56de None\u3002\n    except Exception as e:\n        print(f\"\u53d1\u751f\u9519\u8bef: {e}\")\n        return None\n        # \u6355\u83b7\u5176\u4ed6\u6240\u6709\u5f02\u5e38\uff0c\u6253\u5370\u9519\u8bef\u4fe1\u606f\u5e76\u8fd4\u56de None\u3002\n\nif __name__ == \"__main__\":\n    if not is_admin():\n        ctypes.windll.shell32.ShellExecuteW(None, \"runas\", sys.executable, \" \".join(sys.argv), None, 1)\n    else:\n        current_wifi = get_current_wifi_name()\n        if current_wifi:\n            print(f\"\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi: {current_wifi}\")\n            password = get_wifi_password(current_wifi)\n            if password:\n                print(f\"Wi-Fi\u5bc6\u7801: {password}\")\n            else:\n                print(\"\u672a\u80fd\u83b7\u53d6\u5bc6\u7801\u3002\u8bf7\u786e\u4fdd\u8be5\u7f51\u7edc\u4fdd\u5b58\u4e86\u5bc6\u7801\u3002\")\n        else:\n            print(\"\u65e0\u6cd5\u786e\u5b9a\u5f53\u524d\u8fde\u63a5\u7684Wi-Fi\u3002\")\n    \n    # \u7b49\u5f85\u7528\u6237\u8f93\u5165\u540e\u518d\u5173\u95ed\u7a97\u53e3\n    input(\"\u6309\u56de\u8f66\u952e\u9000\u51fa...\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u662f\u7834\u89e3\u5bc6\u7801\uff0c\u5728\u80fd\u65e0\u7ebf\u4e0a\u7f51\u7684\u60c5\u51b5\u5b50\u4e0b\uff0c\u627e\u51fa\u4fdd\u5b58\u8fc7\u7684\u5bc6\u7801 \u66f4\u591a\u89e3\u91ca\u7684\u4ee3\u7801\uff1aimpo <span class=\"readmore\"><a href=\"http:\/\/xc.ipyingshe.net:5347\/?p=5970\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-5970","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5970"}],"version-history":[{"count":3,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5970\/revisions"}],"predecessor-version":[{"id":5975,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/5970\/revisions\/5975"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5970"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}