netsh_windows命令行管理网络/连接wifi@重启网卡

netsh 命令

  • netsh 是一个针对网络配置shell

refs

获取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.
...
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

  • PS C:\Users\cxxu\Desktop> netsh
    netsh>?
    
    The following commands are available:
    
    Commands in this context:
    ..             - Goes up one context level.
    ?              - Displays a list of commands.
    abort          - Discards changes made while in offline mode.
    add            - Adds a configuration entry to a list of entries.
    advfirewall    - Changes to the `netsh advfirewall' context.
    alias          - Adds an alias.
    branchcache    - Changes to the `netsh branchcache' context.
    bridge         - Changes to the `netsh bridge' context.
    bye            - Exits the program.
    ...
    The following sub-contexts are available:
     advfirewall branchcache bridge dhcpclient dnsclient firewall http interface ipsec lan mbn namespace netio nlm 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 ?.
    

netsh wlan

  • 进入wlan 模式(配置环境context)

  • netsh>wlan
    netsh wlan>?
    
    The following commands are available:
    
    Commands inherited from the netsh context:
    ..             - Goes up one context level.
    abort          - Discards changes made while in offline mode.
    add            - Adds a configuration entry to a list of entries.
    advfirewall    - Changes to the `netsh advfirewall' context.
    alias          - Adds an alias.
    ...
    Commands in this context:
    ?              - Displays a list of commands.
    add            - Adds a configuration entry to a table.
    connect        - Connects to a wireless network.
    ...
    start          - Start hosted network.
    stop           - Stop hosted network.
    
    To view help for a command, type the command, followed by a space, and then
     type ?.
    
    

show(展示当前命令的可有命令)

  • 在这里插入图片描述

检查/扫描当前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 your current connection (if you are using wireless).
  • These connections may not be available from your current location.
正确命令:netsh wlan show networks
  • To list all available connections 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 your current connection (if you are using wireless).
  • These connections may not be available from your current location.

To list all available connections 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.

输出wifi列表🎈

  • 可能会出现乱码(中文字符)

  • 不是实时的,新的信号刚刚进入,可能要等待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
    }
    
  • 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

...

检查/过滤指定wifi是否存在

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

SSID 8 : Redmi Note 11T Pro4

深度刷新/重检查周围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

      • PS C:\Users\cxxu\Desktop> netsh wlan show interfaces|sls Name
        
            Name                   : Wi-Fi
        
      • 常见的Name取值为:

        • wi-fi(无线路由器)
        • wlan(比如手机热点)
        • 下面的脚本已经为你处理好了,所以不需要你自己查看了
      • 检查无线网卡信息获取Name字段的值来获取

      • 大小写应该不区分

强力刷新当前wifi
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 $false
    }
    # Write-Output '😁current environment is @administrator privilege!'
    #write 会影响返回值!
    return $true
}

function wifiList_forceByDisconnect_SudoFirst
{
    <# 
    .synopsis
    务必使用管理员权限运行,否则结果依然不可靠!!!
    #>
    if ($(isAdministratorPrivilege) -eq $False)
    {
        Write-Output '🤣Ops!please try anagin by @Administrator privilege'
        return $False
    }
    else
    {
        Write-Output '😁the current environment is @Administrator privilege'
    }

    Write-Output 'get the current working NIC informations...'
    # netsh wlan show interfaces | Select-String Name
    $Name = (netsh wlan show interfaces | Select-String Name).ToString() -replace '(Name.*):(.*)', '$2'; $Name = $Name.Trim()
    Write-Output "the Name=$Name"
    Write-Output '正在关闭无线网卡(disabling the wlan interface...'
    netsh interface set interface name=$Name admin=disable
    Write-Output 'waiting for the enable operation complete...'
    #需要等待几秒,以便网卡关闭顺利执行(相对耗时,根据自己的情况来调整)
    # Start-Sleep(3)
    # countdown_timer
    Write-Output 'try to enable the interface again ...'
    #重新启动WLAN网卡
    netsh interface set interface name=$Name admin=enable
    Write-Output 'waiting for the enable operation complete...'
    Start-Sleep(0.5)
    Write-Output 'list the current wifi signals...'
    # netsh wlan show networks
    netsh wlan show networks | Select-String ssid
    Write-Output 'the current connected network is:'
    netsh wlan show interfaces | Select-String ^\s*ssid
    
    ping www.baidu.com | Select-Object -First 6
}
调用示例
  • PS C:\Users\cxxu\Desktop> wifiList_forceByDisconnect_SudoFirst
    😁the current environment is @Administrator privilege
    get the current working NIC informations...
    the Name=Wi-Fi
    正在关闭无线网卡(disabling the wlan interface...
    
    waiting for the enable operation complete...
    try to enable the interface again ...
    
    waiting for the enable operation complete...
    list the current wifi signals...
    
    SSID 1 : ChinaNet-95y1520598
    SSID 2 : ChinaNet-sNRv
    SSID 3 : FAST_0770
    SSID 4 : CMCC-user
    the current connected network is:
        SSID                   : ChinaNet-95y15xxxx
    

命令行连接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".
try connect to Redmi Note 11T Pro
Connection request was completed successfully.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
xp下用netsh禁用启用网卡 在XP系统中不支持netsh interface set interface name命令扩展,所以此命令是无法正常运行的,提取2003下的ifmon.dll文件覆盖,就可以用了。 winxp下使用: netsh interface set interface "本地连接" "disabled" 会出现 如下错误提示: 代码: C:\>netsh interface set interface "本地连接" "disabled" 没有指定一个或多个关键性参数 此命令提供的语法不正确。请查看帮助以获取正确的语法信息。 用法 set interface [name = ] IfName [ [admin = ] ENABLED|DISABLED [connect = ] CONNECTED|DISCONNECTED [newname = ] NewName ] 设置接口参数。 IfName - 接口名称 admin - 接口是否被启用 (仅用于非 LAN)。 connect - 是否连接接口 (仅用于非 LAN)。 newname - 接口的新名称 (仅用于非 LAN)。 注意: - 除名称外,还必须至少指定一个选项。 - 如果 connect = CONNECTED 被指定,那么接口被自动 启用,甚至在 admin = DISABLED 选项被指定时也是如 此。 =========================== 替换XP下的ifmon.dll步骤: 但是你可以把win2003系统下 C:\\WINDOWS\\system32\\ifmon.dll文件 拷贝到winxp系统下来使用,从而实现网卡的禁用,启用拷贝时候需要注意:需要先把 ifmon.dll文件拷贝到 C:\\WINDOWS\\system32\\dllcache 替换掉系统文件再拷贝到 C:\\WINDOWS\\system32 替换掉系统文件,若系统会提示 文件被修改了,是否从安装盘还原,选择保持修改的文件就替换成功了。 替换后就可以正常使用netsh命令来禁用,启用网卡了 =========================== 根目录附件中上有win2003的ifmon.dll文件备份 DOS下重启网卡 @ rem 关闭网卡netsh interface set interface name="本地连接" admin=DISABLED @ rem 开启网卡netsh interface set interface name="本地连接" admin=ENABLED 使用PPPOE协议进行宽带拨号时,若要重新拨号,不要对使用PPPOE的“宽带连接netsh,而应该对“本地连接netsh WN7可直接用批处理 如果还不会下载我的附件吧0分。内含有XP用win2003的ifmon.dll文件备份及所用所有批处理文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值