20

How to connect to a new WiFi by enter a password using CMD?

For my school project I have decided to make a WiFi_manager program using cmd.

I know to display all WiFi networks (in cmd):

netsh wlan show networks

Now lets say I want to connect to a WiFi network that I never connected before. And that WiFi is not yet added to profiles.

But I know the password of the WiFi.

1) What will be the command line for that.

Given the information of WiFi network below:

SSID 3 : Ismail
    Network type            : Infrastructure
    Authentication          : WPA-Personal
    Encryption              : CCMP

and password is "Thanks_bro".

If this is not possible, can it be done using C++?

Josef Ginerman
  • 1,460
  • 13
  • 24
Jim Kim.
  • 257
  • 1
  • 3
  • 11

3 Answers3

19

So you already know netsh wlan

If you enter it you get a list of possible commands. One is add.

If you enter netsh wlan add you get another list of possible subcommands. One is profile.

If you enter netsh wlan add profile you get a detailed explanation about all its possible parameters. One needed parameter is a XML file containing the profile informations.

So how to get such an XML file? Go back to netsh wlan and study the keywords. There is export.

If you enter netsh wlan export you get another list of possible subcommands. One is profile. It creates an XML in your local directory containing the needed informations for your current WiFi connection.

If you like to get the password in clear text, you'll also have to add the parameter key=clear. Make the whole command becoming

netsh wlan export profile key=clear

Here is an example which already contains the needed placeholders

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

Simply replace the keywords {SSID} (occurs two times) and {password} with the desired values and import that file by calling

netsh wlan add profile filename="myProfile.xml"
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • 2
    thanks bro got it, may be another day to learn the WHOLE CHAIN of "netsh" commands. But for I am in hurry to finish my project and there are more problems for me to solve and learn too! for now could the steps for me, pls! thx – Jim Kim. Dec 15 '16 at 14:30
  • All the steps are in my answer. Just try them and examine the results. – Oliver Dec 15 '16 at 15:54
  • Bro, this will only show the password of WiFi that have connected before! i want a command the will let me to connect to a new WiFi by typing the password and entering in the cmd.. I hope u got my question. – Jim Kim. Dec 16 '16 at 14:04
  • Write a template file (from a dump file) that contains a placeholder (e.g. `{password}`. Then write a powershell script that asks for the desired parameters ([see this answer](http://stackoverflow.com/questions/8184167)) and replace the text in the template file ([see this answer](http://stackoverflow.com/questions/60034)). Then use the `netsh wlan add profile` call to add it to the system. – Oliver Dec 16 '16 at 14:22
  • I already HAD that idea. It failed, Two problem: 1) In xml file, the system stores the wifi password in encrypted form. so i have to do convert my password to that form and save it as xml, which not possible to convert. 2) Windows defender catchs my files as harmful. – Jim Kim. Dec 16 '16 at 15:53
  • If you call the export command with `key=clear` you get an example file which contains the password in clear text. The node `protected` is `false` and the node `keyMaterial` contains the key in clear text. – Oliver Dec 19 '16 at 07:13
  • Thx, Bro! i go !! Also pls visit:http://superuser.com/questions/1157485/using-cmd-batch-how-to-determine-whether-or-not-you-are-connected-to-a-wifi-net?noredirect=1#comment1666735_1157485 . And try to do ur help. – Jim Kim. Dec 19 '16 at 14:56
  • How can I enter username and passoword for enterprise WiFi? – JPX Dec 05 '19 at 07:34
  • @JPX: What declares a WiFi to be *enterprise*? And what is the different from a client point of view? Either ask a new question and describe your differences or configure the WiFi through Windows as usual and export the profile as described above. – Oliver Dec 05 '19 at 11:13
  • @Oliver Enterprise WiFi uses WPA2-Enterprise not WPA2-PSK. WPA2-Enterprise protocol PEAP-MSCHAPv2 requires username and password. Thill will provide secure access for multiple users (unlike WPA2-PSK because WPA2-PSK password need to known by all users). RADIUS server will handle the authentcation which is based on 802.1X. Username and password are not included in profile XML file. That's why your solution doesn't work. – JPX Dec 05 '19 at 20:28
  • IMHO if you are setup an *enterprise* you should be able to deploy GPOs to your users and their machine. AFAIK it is possible to configure e.g. with Intune how these machines should connect and set all the needed things to connect to your WiFi, but that's something I don't know and maybe worth a new question (but maybe on server fault or super user). – Oliver Dec 06 '19 at 10:37
  • @Oliver I created a new question about that. There's no way to set a username and password in Intune. https://serverfault.com/questions/994281/microsoft-intune-how-to-deploy-enterprise-wifi-profile-with-username-and-passw – JPX Dec 06 '19 at 15:19
0

A basic netsh wlan ? at a command prompt shows that there is a netsh wlan connect command. However, it appears that this command requires a pre-existing "profile"; you would need to create that using netsh wlan add.
The details are left as an exercise for the reader. (It is homework, after all.)

There is also a sample WLAN client in C/C++ using the Windows API included in the Windows SDK. I found this by searching for wlanclient msdn, the page is here.

David
  • 2,226
  • 32
  • 39
-4

on a Mac you can use this line of bash in Terminal to log into a wifi network:

networksetup -setairportnetwork port networkname password

Note:

port is your wifi port (on my Mac it's port en0)

networkname is the name of the network, like Starbucks

password is just the straight up password for the network

if the password is already saved in your keychain you don't need that param

this should work on other systems too

Daniel McGrath
  • 454
  • 5
  • 6