How to connect to a WiFi using CMD only?

2019-03-11 15:21发布

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++?

3条回答
Viruses.
2楼-- · 2019-03-11 16:05

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"
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-11 16:10

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.

查看更多
小情绪 Triste *
4楼-- · 2019-03-11 16:10

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

查看更多
登录 后发表回答