4

So, I am currently trying to create a batch file to connect to a wireless network. So far I have the following...

@echo off
netsh wlan connect ssid="My SSID" name="My Name"
pause

It works fine, but the issue is that it can only connect to networks that are already in my profiles. Is there any way that I can connect to a wireless network, using a password as an argument, that is not already in my profiles?

T. Gibbons
  • 4,919
  • 2
  • 15
  • 32

2 Answers2

3

You need a xml file that has the SSID and password.

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>{example}</name>
    <SSIDConfig>
        <SSID>
            <hex>{6578616d706c65}</hex>
            <name>{example}</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>{password}</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <MacRandomization 
xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
        <enableRandomization>false</enableRandomization>
    </MacRandomization>
</WLANProfile>

Fill in the {6578616d706c65}, {example}, {example} and {password} with your own information. {6578616d706c65} is the Hex of example, click here to convert ASCII to HEX.

Make sure you use the correct format, windows WILL NOT accept even there's one more space at the end.

click here to download example.xml and other files.




If you want to use pure cmd to connect (without changing the {password} manually), keep reading


To do this, you will need 36 xml files with a-z and 0-9 (it's not possible to have both caps and lowercase.)

click here to download the 36 files and example.xml.

First you need to separate example.xml into 3 parts, the first part is from <?xml version="1.0"?> to <keyMaterial>. Name that to T.xml.

The second part is the password.

The third part is from </keyMaterial> to the end (</WLANProfile>) make sure you don't forget the line break after </WLANProfile>. Name that to B.xml.

Then you will use the copy command to combine the files.

The code should look like this

copy /y C:\T.xml + C:\keyMaterial\p.xml + C:\keyMaterial\a.xml + C:\keyMaterial\s.xml + C:\keyMaterial\s.xml + C:\keyMaterial\w.xml + C:\keyMaterial\o.xml + C:\keyMaterial\r.xml + C:\keyMaterial\d.xml + C:\B.xml C:\example.xml /B
netsh wlan add profile filename="C:\example.xml" user=all
netsh wlan connect example

Finally, you can check if you're connected by running ping google.com.

Benjamin2002
  • 311
  • 1
  • 4
  • 13
0

Not with a single netsh command. You'll need to have a profile for the network. If you know the security settings of the network you would like to connect to, your batch file can use netsh to import a profile with the SSID/password you want to connect, then use netsh to connect to that profile. Your batch file would have to assemble the profile xml. However, this approach is very brittle because if a network does not match the security settings in your generated profile, the connection won't succeed.

You could attempt to use other commands of netsh to infer the security settings and try to cobble together a profile to match an arbitrary network, but that opens a whole new can of worms.

I can't really recommend either approach, but if you want to export/import profiles, the commands are

netsh wlan export profile "<profile name here>"
netsh wlan add profile <xml filename here>
alexcalibur
  • 384
  • 1
  • 7
  • Just keep in mind that this will work except that you must be using cmd as an administrator or else it will not allow you to do these commands. –  Nov 27 '17 at 22:14