1

I try the joys of netsh wlan under win.10. So basically:

  1. I have an XML file called virgin.xml, which is, as you can, guess "virgin". The file:
<?xml version='1.0' encoding='utf-8'?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1" 
                 xmlns:ns1="http://www.microsoft.com/networking/WLAN/profile/v3">
    <name>HERE THE NAME IS EMPTY, THIS IS WHY THE FILE IS CALLED "VIRGIN"</name>
    <SSIDConfig>
        <SSID>
            <name>ALSO HERE</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>false</protected>
                <keyMaterial>THE PASSWORD IS ALSO EMPTY</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <ns1:MacRandomization>
        <ns1:enableRandomization>false</ns1:enableRandomization>
    </ns1:MacRandomization>
</WLANProfile>

And with a script I modify the file to enter the name of my wifi (antigon here) and the password. So now my "virgin.xml" isn't a virgin anymore, it's a classic wifi profile XML with a name and a password given inside.

  1. I do netsh wlan add profile filename = "C:\virgin.xml" to add a profile from an XML file

  2. I added a profile, however if I do netsh wlan show profiles, there is no "virgin" profile. I deduce that it created a profile with the name of the wifi indicated in the XML file, antigon. There was already one called antigon, the basic one because it is my wifi so I already connected into it, and when I look at the profiles, there is still only one antigon. Conclusion since I did not have an error message: it modified the information of the already existing antigon profile with those given in the XML file, among others the password (maybe this is a point where I am wrong)

  3. I do netsh wlan connect according to the syntax given on a site: netsh wlan connect ssid = YOURSSID name = PROFILENAME to connect into a wifi network giving the name and the profile (so that it gets the password). So for me: netsh wlan connect ssid = antigon name = antigon

  4. "The connection request succeeded", it connects me.

  5. I change the password in the XML file to put a false one and I repeat steps 2 to 4, "The connection request was successful", and it still connects me ! (This is why I said that it could be a point where I am wrong at step 3 : maybe it didn't change the password and so it's pretty logic to still connect ?)

Does anyone know where the error is? It's annoying, I have all the elements in hand to do what I want but I can't manage to fit them correctly ! What I would like to do is when I put the right password in the XML file and run netsh wlan export and then netsh wlan connect, it connects me, and it does not work if I put the wrong password.

*What I can also do : I call batch files from a python script, which is my main script. So if you got a solution to connect to a wifi using Python, like, I know there are libraries like os that allow using cmd commands, I take them!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Quantum Sushi
  • 504
  • 7
  • 23
  • Does https://stackoverflow.com/questions/56721759/how-to-connect-to-wifi-network-on-windows-using-python-3/56722059#56722059 help? – CristiFati May 14 '20 at 21:28
  • @CristiFati it could work but this is complicated... And in fact I really feel stupid cause I've been searching for days, finally decided to put a bounty, and LITERALLY 5 minutes after posting that bounty... I found the answer. I hate myself. – Quantum Sushi May 15 '20 at 06:18
  • Maybe putting the bounty was the trigger that unlocked the problem solving. don;t feel bad. At least you got some medal :). Anyway you should answer your own question. – CristiFati May 15 '20 at 07:29
  • Yeah that's what i'm actually doing haha :D Ok thanks for help anyway ! – Quantum Sushi May 15 '20 at 07:31

1 Answers1

1

Here is the answer: how to connect to a Wireless Network using Python, and giving a password ?

What we will do is calling a batch (.bat) file, wich is the equivalent in a text file of cmd commands, with our Python script. I'm using the last version of Python (3.8) and windows 10.

IF YOU NEVER CONNECTED INTO THAT WIFI (ignore else):

netsh wlan export profile name = A_WIFI_YOU_ALREADY_CONNECTED_INTO

So you can get an XML file. Then open it, and modify every time "name" appears to replace the value by the name of the wifi you want to connect in :

<?xml version='1.0' encoding='utf-8'?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1" xmlns:ns1="http://www.microsoft.com/networking/WLAN/profile/v3">
    <name>HERE THE NAME</name>
    <SSIDConfig>
        <SSID>
            <name>HERE THE NAME</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>false</protected>
                <keyMaterial>HERE IS YOUR PASSWORD, DON'T CHANGE IT, WE WILL DO IT LATER</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <ns1:MacRandomization>
        <ns1:enableRandomization>false</ns1:enableRandomization>
    </ns1:MacRandomization>
</WLANProfile>

Then :

netsh wlan add profile filename = PATH_TO_THE_XML

Ok so now you got your profile, let's see what's next (start form here if you already connected into the wifi once) :

netsh wlan set profileparameter name=YOUR_WIFI keyMaterial = YOUR_KEY
netsh wlan connect name=YOUR_PROFILE_NAME(usually same as wifi name) ssid=YOUR_WIFI

First line will change the password, second one will connect you.

Note : even if the password is wrong, it will say that you connected, but it won't connect you. So don't read the output to check if you are connected...

Last step : how to use this inside a python script ? 2 options :

  1. Use the OS library to call cmd commands :
import os
os.popen('your command here')

(Don't forget to install os with pip first)

  1. Use a batch file:

Create a new txt file, write all your commands inside, line-by-line, and name it "connect.bat". Then create a python script:

import subprocess
def connect():
    subprocess.call([r"path where the batch file is stored\name of the batch file.bat if it is in the same directory (don't need to specify the path)"])
halfer
  • 19,824
  • 17
  • 99
  • 186
Quantum Sushi
  • 504
  • 7
  • 23