【一文搞懂】【CMD】netsh用法整理

Netsh 是命令行脚本实用工具,可让你显示或修改当前正在运行的计算机的网络配置。 可以通过在 netsh 提示符下键入命令来运行 Netsh 命令,并且可以在批处理文件或脚本中使用 Netsh 命令。 可以使用 netsh 命令来配置远程计算机和本地计算机。

Netsh 还提供脚本功能,可让你在批处理模式下对指定的计算机运行一组命令。 你可以使用 Netsh 将配置脚本保存在文本文件中,以便存档或者帮助你配置其他计算机。

一、如何正确使用netsh

  1. 当我们忘记netsh的相关命令时,可以直接使用netsh自带的帮助功能,就能看到一些可以使用的命令。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
netsh /?

C:\Users\ming Ming>netsh /?

Usage: netsh [-a AliasFile] [-c Context] [-r RemoteMachine] [-u [DomainName\]UserName] [-p Password | *]
[Command | -f ScriptFile]

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.
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.
nlm - Changes to the `netsh nlm' 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 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 ?.
  1. 那知道有哪些可选功能后,我们就可以进一步输入二级命令去执行我们将要进行的操作,比如下边我就以查看当前tcp链接状态位列展示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    $>netsh
    $netsh>int
    $netsh interface>ip
    $netsh interface ipv4>show tcpstats

    TCP Statistics
    ------------------------------------------------------
    Timeout Algorithm: Van Jacobson's Algorithm
    Minimum Timeout: 5
    Maximum Timeout: 4294967295
    Maximum Connections: Dynamic
    Active Opens: 3971
    Passive Opens: 556
    Attempts Failed: 1017
    Established Resets: 407
    Currently Established: 34
    In Segments: 202193
    Out Segments: 149657
    Retransmitted Segments: 0
    In Errors: 0
    Out Resets: 845
    Fastopen Active Opens: 0
    Fastopen Passive Opens: 0
    Fastopen Attempts Failed: 0
    Retransmits Of First SYN: 644
    Retransmits Of First SYN (Fastopen): 0

二、netsh命令整理

1. 导出导入脚本

  1. 导出配置脚本:

    1
    netsh -c interface ip dump > c:/interface.txt
  2. 导入配置脚本:

    1
    netsh -f c:/interface.txt

2. 固定IP地址批处理

//SETIPDNS.bat( “本地连接”换成你网卡的名字, 具体的 ip 地址,网关,请自行修改 )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@echo off

echo 正在修改本机IP...

netsh interface ip set address name="本地连接" source=static 192.168.1.188 255.255.255.0 192.168.1.1 1

echo 正在添加本机主DNS...

netsh interface ip set dns "本地连接" static 18.0.0.10 primary

echo 正在添加本机副DNS...

netsh interface ip add dns "本地连接" 18.0.0.10

echo 设置完成...

echo 检查当前本机配置...

ipconfig /all

echo 导出配置脚本...

netsh -c interface ip dump

pause

3. 自动获取 IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@echo off

echo 正在修改本机IP...

netsh interface ip set address name="本地连接" source=dhcp

echo 正在添加本机主DNS...

netsh interface ip set dns "本地连接" dhcp

echo 设置完成...

echo 检查当前本机配置...

ipconfig /all

echo 导出配置脚本...

netsh -c interface ip dump

pause

4. netsh用法

  1. 查看网络配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    netsh interface ip show {选项}

    {选项}可以是:
    address-显示IP地址配置。
    config-显示IP地址和更多信息。
    dns-显示DNS服务器地址。
    icmp-显示ICMP统计。
    interface-显示IP接口统计。
    ipaddress-显示当前IP地址。
    ipnet-显示IP的网络到媒体的映射。
    ipstats-显示IP统计。
    joins-显示加入的多播组。
    offload-显示卸载信息。
    tcpconn-显示TCP连接。
    tcpstats-显示TCP统计。
    udpconn-显示UDP连接。
    udpstats-显示UDP统计。
    wins-显示WINS服务器地址。
  2. 查看网络配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    netsh interface ip show {选项}

    {选项}可以是:
    address-显示IP地址配置。
    config-显示IP地址和更多信息。
    dns-显示DNS服务器地址。
    icmp-显示ICMP统计。
    interface-显示IP接口统计。
    ipaddress-显示当前IP地址。
    ipnet-显示IP的网络到媒体的映射。
    ipstats-显示IP统计。
    joins-显示加入的多播组。
    offload-显示卸载信息。
    tcpconn-显示TCP连接。
    tcpstats-显示TCP统计。
    udpconn-显示UDP连接。
    udpstats-显示UDP统计。
    wins-显示WINS服务器地址。
  3. 配置接口IP/网关IP

    1
    netsh interface ip set address "本地连接" static 10.1.2.90 255.255.255.0 10.1.2.254 1
  4. 配置自动换取IP地址,DNS地址及wins地址

    1
    2
    3
    netsh interface ip set address "本地连接" dhcp
    netsh interface ip set dns "本地连接" dhcp
    netsh interface ip set wins "本地连接" dhcp
  5. 配置静态IP地址,DNS地址及wins地址

    1
    2
    3
    netsh interface ip set address "本地连接" static 10.1.2.90
    netsh interface ip set dns "本地连接" static 202.99.160.68
    netsh interface ip set wins "本地连接" static 10.1.2.200
  6. 看网络配置文件

    1
    C:>netsh -c interface dump
  7. 导出网络配置文件

    1
    netsh -c interface dump > d:/1.txt
  8. 导入网络配置文件

    1
    2
    netsh -f d:/1.txt
    netsh exec d:/2.txt
  9. netsh 配置防火墙
    查看、开启或禁用系统防火墙

    1
    2
    3
    4
    5
    6
    7
    8
    # 查看防火墙的状态
    netsh firewall show state

    # 禁用系统防火墙
    netsh firewall set opmode disable

    # 启用防火墙
    netsh firewall set opmode enable

【一文搞懂】【CMD】netsh用法整理
https://hodlyounger.github.io/A_OS/Windows/cmd/netsh用法整理/
作者
mingming
发布于
2023年10月27日
许可协议