windows 如何仅使用 CMD 连接到 WiFi?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41163737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:45:32  来源:igfitidea点击:

How to connect to a WiFi using CMD only?

windowsbatch-filecmd

提问by Jim Kim.

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

如何通过使用 CMD 输入密码连接到新的 WiFi?

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

对于我的学校项目,我决定使用 cmd 制作一个 WiFi_manager 程序。

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

我知道要显示所有 WiFi 网络(在 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.

现在假设我想连接到我以前从未连接过的 WiFi 网络。并且该 WiFi 尚未添加到配置文件中。

But I know the password of the WiFi.

但是我知道WiFi的密码。

1) What will be the command line for that.

1)什么是命令行。

Given the information of WiFi network below:

鉴于以下 WiFi 网络信息:

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

如果这是不可能的,可以使用 C++ 来完成吗?

回答by Oliver

So you already know netsh wlan

所以你已经知道了 netsh wlan

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

如果你输入它,你会得到一个可能的命令列表。一个是add

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

如果您输入,netsh wlan add您将获得另一个可能的子命令列表。一个是profile

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

如果您输入,netsh wlan add profile您将获得有关其所有可能参数的详细说明。一个需要的参数是包含配置文件信息的 XML 文件。

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

那么如何获取这样的XML文件呢?返回netsh wlan并研究关键字。有export

If you enter netsh wlan exportyou 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.

如果您输入,netsh wlan export您将获得另一个可能的子命令列表。一个是profile。它会在您的本地目录中创建一个 XML,其中包含您当前 WiFi 连接所需的信息。

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

如果您想以明文形式获取密码,则还必须添加参数key=clear。使整个命令成为

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

只需替换关键字{SSID}(出现两次)和{password}所需的值并通过调用导入该文件

netsh wlan add profile filename="myProfile.xml"

回答by David

A basic netsh wlan ?at a command prompt shows that there is a netsh wlan connectcommand. 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.)

netsh wlan ?命令提示符处的基本信息显示有一个netsh wlan connect命令。但是,该命令似乎需要预先存在的“配置文件”;您需要使用netsh wlan add.
细节留给读者作为练习。(毕竟这是家庭作业。)

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.

还有一个使用 Windows SDK 中包含的 Windows API 的 C/C++ 示例 WLAN 客户端。我通过搜索wlanclient msdn找到了这个,页面在这里

回答by Daniel McGrath

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

在 Mac 上,您可以在终端中使用这行 bash 登录到 wifi 网络:

networksetup -setairportnetwork port networkname password

Note:

笔记:

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

端口是您的 wifi 端口(在我的 Mac 上是端口 en0)

networknameis the name of the network, like Starbucks

networkname是网络的名称,如星巴克

passwordis 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

这也应该适用于其他系统