天天看點

netsh_windows指令行管理網絡/連接配接wifi

文章目錄

  • ​​netsh 指令​​
  • ​​refs​​
  • ​​擷取netsh幫助​​
  • ​​對比Cisco packet tracer CLI​​
  • ​​相同點(類似)​​
  • ​​不同​​
  • ​​使用netsh​​
  • ​​netsh wlan​​
  • ​​show(展示目前指令的可有指令)​​
  • ​​檢查/掃描目前wifi信号​​
  • ​​How can I display the other wireless connections?​​
  • ​​不适合用來掃描可用信号的指令​​
  • ​​正确指令:netsh wlan show networks​​
  • ​​How can I display the other wireless connections?​​
  • ​​輸出清單分析​​
  • ​​檢查/過濾指定wifi是否存在​​
  • ​​深度重新整理/重檢查周圍wifi清單​​
  • ​​指令行連接配接wifi​​
  • ​​powershell函數連接配接指定wifi​​
  • ​​斷開目前wifi​​
  • ​​重新連接配接到指定wifi​​

netsh 指令

  • netsh 是一個針對網絡配置shell

refs

  • ​​Network Shell (Netsh) | Microsoft Docs​​
  • ​​netsh commands | How does netsh work? [+examples] - IONOS​​
  • ​​Manage WiFi connection in Windows 10 with PowerShell | 4sysops​​
  • ​​How to disable WiFi using CMD or Powershell in Windows 10 (thewindowsclub.com)​​
  • ​​windows - How do I display a list of Wi-Fi connections using netsh - Super User​​

擷取netsh幫助

PS D:\repos\scripts> netsh help

The following commands are available:

Commands in this context:
?              - Displays a list of commands.
add            - Adds a configuration entry to a list of entries.
advfirewall    - Changes to the `netsh advfirewall' context.
branchcache    - Changes to the `netsh branchcache' context.
bridge         - Changes to the `netsh bridge' context.
delete         - Deletes a configuration entry from a list of entries.
dhcpclient     - Changes to the `netsh dhcpclient' context.
dnsclient      - Changes to the `netsh dnsclient' context.
dump           - Displays a configuration script.
exec           - Runs a script file.
firewall       - Changes to the `netsh firewall' context.
help           - Displays a list of commands.
http           - Changes to the `netsh http' context.
interface      - Changes to the `netsh interface' context.
ipsec          - Changes to the `netsh ipsec' context.
lan            - Changes to the `netsh lan' context.
mbn            - Changes to the `netsh mbn' context.
namespace      - Changes to the `netsh namespace' context.
netio          - Changes to the `netsh netio' context.
p2p            - Changes to the `netsh p2p' context.
ras            - Changes to the `netsh ras' context.
rpc            - Changes to the `netsh rpc' context.
set            - Updates configuration settings.
show           - Displays information.
trace          - Changes to the `netsh trace' context.
wcn            - Changes to the `netsh wcn' context.
wfp            - Changes to the `netsh wfp' context.
winhttp        - Changes to the `netsh winhttp' context.
winsock        - Changes to the `netsh winsock' context.
wlan           - Changes to the `netsh wlan' context.

The following sub-contexts are available:
 advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec lan mbn namespace netio p2p ras rpc trace wcn wfp winhttp winsock wlan

To view help for a command, type the command, followed by a space, and then
 type ?.      

對比Cisco packet tracer CLI

相同點(類似)

  • netsh 模式和cisco packet tracer 中的CLI的操作有些類似
  • 比如基于上下文提供不同的可用指令
  • 擷取幫助的方式

不同

  • netsh 似乎不支援指令補齊
  • netsh 但是還是可以在不發生歧義的情況,隻輸入單詞的前幾個字元

使用netsh

  • netsh_windows指令行管理網絡/連接配接wifi

netsh wlan

  • 進入​

    ​wlan​

    ​ 模式(配置環境context)
  • netsh_windows指令行管理網絡/連接配接wifi

show(展示目前指令的可有指令)

  • netsh_windows指令行管理網絡/連接配接wifi

檢查/掃描目前wifi信号

How can I display the other wireless connections?

不适合用來掃描可用信号的指令

  • command​

    ​netsh wlan show profiles​

    ​ through command prompt shows list of all the all Wi-Fi available at that time to which my computer can connect.

The above command shows only those connections which have profiles.

  • They are connections you have previously connected to and have saved configurations.
  • It will always show yourcurrentconnection (if you are using wireless).
  • These connections may not be available from your current location.

正确指令:netsh wlan show networks

  • To list allavailableconnections you need to use a different​

    ​netsh​

    ​ command:
netsh wlan show networks      
  • These connections are available at your current location.
  • You must be disconnected from all wireless networks before running this command.

Example output (names have been obscured):

f:\test>netsh wlan show networks

Interface name : Wireless Network Connection
There are 22 networks currently visible.

SSID 1 : SKYXXXXX
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 2 : SKYXXXXX
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 3 : XXXXX
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 4 : BTOpenzoneXXX
    Network type            : Infrastructure
    Authentication          : Open
    Encryption              : None

...      

How can I display the other wireless connections?

command ​

​netsh wlan show profiles​

​ through command prompt shows list of all the all Wi-Fi available at that time to which my computer can connect.

The above command shows only those connections which have profiles.

  • They are connections you have previously connected to and have saved configurations.
  • It will always show yourcurrentconnection (if you are using wireless).
  • These connections may not be available from your current location.
netsh wlan show networks      
  • These connections are available at your current location.
  • You must be disconnected from all wireless networks before running this command.

輸出清單分析

  • 可能會出現亂碼(中文字元)
  • 不是實時的,新的信号剛剛進入,可能要等待5s左右才可以掃描到
  • 總之,這種方法并不可靠!
  • 必須斷開所有wifi後再執行該指令,否則隻傳回目前已連接配接的wifi
function wifi_list
{
    Write-Output 'list the current wifi signals...'
    Write-Output 'if there is only one item returned,please disconnect current wifi!🎈'
    netsh wlan show networks
}      
f:\test>netsh wlan show networks

Interface name : Wireless Network Connection
There are 22 networks currently visible.

SSID 1 : SKYXXXXX
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 2 : SKYXXXXX
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 3 : XXXXX
    Network type            : Infrastructure
    Authentication          : WPA2-Personal
    Encryption              : CCMP

SSID 4 : BTOpenzoneXXX
    Network type            : Infrastructure
    Authentication          : Open
    Encryption              : None

...      

檢查/過濾指定wifi是否存在

PS D:\repos\scripts> wifi_list|sls "11T"

SSID 8 :      

深度重新整理/重檢查周圍wifi清單

  • 🎈🎈🎈使用管理者方式代開cmd/powershell
  • 先禁用WLAN網卡
netsh interface set interface name="wLAN" admin=disable      
  • 根據具體情況,也可能是​

    ​netsh interface set interface name="wi-fi" admin=disable​

  • 可以通過:​

    ​netsh wlan show interfaces​

  • 檢查無線網卡資訊擷取Name字段的值來擷取
  • 大小寫應該不區分
function isAdministratorPrivilege
{
    if (!([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544'))
    {
        Write-Output '🎈🎈🎈🎈🎈🎈🎈🎈🎈warning!🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈'
        Write-Output '🤣😂😊current powershell run without administrator privilege!;請手動打開管理模式的terminal.'
        return 0
    }
    return 1
}

function wifiList_forceByDisconnect
{
    <# 
    .synopsis
    務必使用管理者權限運作,否則結果依然不可靠!!!
    #>
    if (!(isAdministratorPrivilege))
    {
        Write-Output 'please try anagin'
        return False
    }

    netsh interface set interface name="wlan" admin=disable
    Write-Output '正在關閉無線網卡(disabling the wlan interface...'
    #需要等待幾秒,以便網卡關閉順利執行(相對耗時,根據自己的情況來調整)
    Start-Sleep(4)
    Write-Output 'try to enable the interface again ...'
    #重新啟動WLAN網卡
    netsh interface set interface name="wlan" admin=enable
    Start-Sleep(0.5)
    Write-Output 'list the current wifi signals...'
    netsh wlan show networks
}      

指令行連接配接wifi

  • 常用wifi的連接配接通過可以考慮使用CLI進行

powershell函數連接配接指定wifi

  • 首次連接配接需要wifi輸入密碼
function connectWlan
{
    param(
        $ssid = "WifiNameByYou"
    )
    Write-Output "try connecting to wifi ssid:$ssid"
    netsh wlan connect name=$ssid
}      

斷開目前wifi

  • 某些時候我們要重新掃描可用wifi,或者其他原因,可以嘗試先斷開目前wifi
  • ​netsh wlan disconnect​

重新連接配接到指定wifi

  • 下面的函數嘗試斷開目前的wifi,并重新連接配接到指定的wifi
function wifi_wlan_reconnect_to {
    param(
        $ssid = 'YourFavoriteWifiName'
    )
    Write-Output 'try disconnect current wifi🎈...'
    netsh wlan disconnect
    Write-Output "try connect to $ssid"
    netsh wlan connect name=$ssid
}      
  • 連接配接示例:
PS D:\repos\scripts> wifi_wlan_reconnect_to "Redmi Note 11T Pro"
try disconnect current wifi🎈...
Disconnection request was completed successfully for interface "WLAN".      

繼續閱讀