欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

我自己亲自做的 根据 无线网 wifi 的名称和密码来自动连接 个人级别的 wifi 网络 有大用 有大大用 有大大大用 有大大大大用

下面生成的 WLANProfille 文件前面不能有空格

f.write(f"""<?xml version="1.0"?>   这里要顶格写,<?xml version="1.0"?> 不能另起一行

比如如下就是不对的 f.write(f"""

<?xml version="1.0"?>


</WLANProfile>""")   这里不能在</WLANProfile>后面加上回车键,再三个双引号

比如如下就是不对的 </WLANProfile>

"""



密码未加密的方式  OK 

import subprocess
import os
import ctypes
def connect_to_wifi(ssid, password):
    try:
        hex_representation = "".join([hex(ord(ch))[2:] for ch in ssid])
        # 创建 Wi-Fi 配置文件    
        command1 = f'netsh wlan add profile filename="{ssid}.xml"'    
        with open(f'{ssid}.xml', 'w') as f:
            f.write(f"""<?xml version="1.0"?>    
    <name>{ssid}</name>    
    <SSIDConfig>    
        <SSID>    
            <name>{ssid}</name>    
        </SSID>    
    </SSIDConfig>    
    <connectionType>ESS</connectionType>    
    <connectionMode>manual</connectionMode>    
    <MSM>    
        <security>    
            <authEncryption>    
                <authentication>WPA2PSK</authentication>    
                <encryption>AES</encryption>    
                <useOneX>false</useOneX>    
            </authEncryption>    
            <sharedKey>    
                <keyType>passPhrase</keyType>    
                <protected>false</protected>    
                <keyMaterial>{password}</keyMaterial>    
            </sharedKey>    
        </security>    
    </MSM>    
</WLANProfile>""")

   
        subprocess.run(command1, shell=True, check=True)   #check=true是检查有没有错误的

   
        # 连接到 Wi-Fi    
        command2 = f'netsh wlan connect name="{ssid}"'    
        subprocess.run(command2, shell=True, check=True)  #check=true是检查有没有错误的

   
        print(f'Successfully connected to {ssid}')
        ctypes.windll.user32.MessageBoxW(0, f"成功连接wifi {ssid}!", "提示", 0)  # 弹出窗口示例    
    except Exception as e:
        ctypes.windll.user32.MessageBoxW(0, f"未能成功连接wifi {ssid}!", "提示", 0)  # 弹出窗口示例    
        print(f'Error connecting to Wi-Fi: {e}')


   
# 使用示例    
ssid = "aaaa"    
password = "12345678"    
wlan_command = f"netsh wlan delete profile name =\"{ssid}\"" + " interface = WLAN"    
os.system(wlan_command)
connect_to_wifi(ssid, password)



密码加密的方式  OK 


import subprocess
import os
import ctypes
def connect_to_wifi(ssid, password):
    try:
        hex_representation = "".join([hex(ord(ch))[2:] for ch in ssid])
        # 创建 Wi-Fi 配置文件    
        command1 = f'netsh wlan add profile filename="{ssid}.xml"'    
        with open(f'{ssid}.xml', 'w') as f:
            f.write(f"""<?xml version="1.0"?>    
    <name>{ssid}</name>    
    <SSIDConfig>    
        <SSID>    
            <hex>{hex_representation}</hex>    
            <name>{ssid}</name>    
        </SSID>    
    </SSIDConfig>    
    <connectionType>ESS</connectionType>    
    <connectionMode>auto</connectionMode>    
    <MSM>    
        <security>    
            <authEncryption>    
                <authentication>WPA2PSK</authentication>    
                <encryption>AES</encryption>    
                <useOneX>false</useOneX>    
            </authEncryption>    
            <sharedKey>    
                <keyType>passPhrase</keyType>    
                <protected>true</protected>    
                <keyMaterial>01000000D08C9DDF0115D1118C7A00C04FC297EB01000000D4BF250214D031499824B39DC675C8120000000002000000000010660000000100002000000027C7B46F4A84D2D3E1E506F713D7ECA8EEF6CFAA7F71EBEC8FDEEA5B6D69DF18000000000E8000000002000020000000AA910CC590BEE6BEFC90BBE4B17604BB9AF8F6011C70821010C031A0771370D31000000059F6218DA84B8DBF3E831E8D3915ECBF400000005590C12D55576C961B5D159228FAA377F4279E8897484D506ED097F5CB6A24531335A59F74FC707A4812BD6C3BFB56713F86DE0D44CCE739E68BE97EEEADEA0A</keyMaterial>    
            </sharedKey>    
        </security>    
    </MSM>    
    <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">    
        <enableRandomization>false</enableRandomization>    
        <randomizationSeed>2351468514</randomizationSeed>    
    </MacRandomization>    
</WLANProfile>""")

   
        subprocess.run(command1, shell=True, check=True) #check=true是检查有没有错误的

   
        # 连接到 Wi-Fi    
        command2 = f'netsh wlan connect name="{ssid}"'    
        subprocess.run(command2, shell=True, check=True) #check=true是检查有没有错误的

   
        print(f'Successfully connected to {ssid}')
        ctypes.windll.user32.MessageBoxW(0, f"成功连接wifi {ssid}!", "提示", 0)  # 弹出窗口示例    
    except Exception as e:
        ctypes.windll.user32.MessageBoxW(0, f"未能成功连接wifi {ssid}!", "提示", 0)  # 弹出窗口示例    
        print(f'Error connecting to Wi-Fi: {e}')

   
# 使用示例    
ssid = "aaaa"    
password = "12345678"    
wlan_command = f"netsh wlan delete profile name =\"{ssid}\"" + " interface = WLAN"    
os.system(wlan_command)

   
connect_to_wifi(ssid, password)



中文无线网 密码加密的方式(注意代码中红色的部分,表示以utf-8的格式写入文件)  OK 

#!/usr/bin/python3
#
import subprocess
import os
import ctypes
def connect_to_wifi(ssid, password):
    try:
        #hex_representation = "".join([hex(ord(ch))[2:] for ch in ssid])
        # 创建 Wi-Fi 配置文件
        command1 = f'netsh wlan add profile filename="{ssid}.xml"'
        with open(f'{ssid}.xml', 'w',encoding='utf-8') as f:
            f.write(f"""<?xml version="1.0"?>
    <name>小米 10 Pro</name>
    <SSIDConfig>
        <SSID>
            <hex>E5B08FE7B1B32031302050726F</hex>
            <name>小米 10 Pro</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>true</protected>
                <keyMaterial>01000000D08C9DDF0115D1118C7A00C04FC297EB01000000F11D019D2651714599F66C128F0C38810000000002000000000010660000000100002000000041DC940BF8A6BABC2546EF4E184B6EEF8B361D1E5C56E1F1D12286DCB88B4D6E000000000E8000000002000020000000777BC2454ED53249A96D241ACFEBC82A50664EE8D3D6A181E6B6D9D3FEE72110100000004666CECF651E3859BE5C0A363E6B8006400000008021D5053DC8C64FC858B78C5C1C0B04AA378D8DAA9886FD34DD67959B06F2FF764A61F6CC359F23099DB5A2BBDCBE25564DFB5F86751E8042B1195D42F7725B</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
        <enableRandomization>false</enableRandomization>
        <randomizationSeed>3087350222</randomizationSeed>
    </MacRandomization>
</WLANProfile>
""")
        subprocess.run(command1, shell=True, check=True)  # check=true是检查有没有错误的
        # 连接到 Wi-Fi
        command2 = f'netsh wlan connect name="{ssid}"'
        subprocess.run(command2, shell=True, check=True)  # check=true是检查有没有错误的
        print(f'Successfully connected to {ssid}')
        ctypes.windll.user32.MessageBoxW(0, f"成功连接wifi {ssid}!", "提示", 0)  # 弹出窗口示例
    except Exception as e:
        ctypes.windll.user32.MessageBoxW(0, f"未能成功连接wifi {ssid}!", "提示", 0)  # 弹出窗口示例
        print(f'Error connecting to Wi-Fi: {e}')
# 使用示例
ssid = "小米 10 Pro"
password = "123456789"
wlan_command = f"netsh wlan delete profile name=\"{ssid}\"" + " interface=WLAN"
os.system(wlan_command)
connect_to_wifi(ssid, password)


普通分类: