Windows自带netsh命令

微软Windows的netsh是一个命令行脚本实用工具。使用netsh工具 ,可以查看或更改本地计算机或远程计算机的网络配置。不仅可以在本地计算机上运行这些命令,而且可以在网络上的远程计算机上运行。

1、端口转发

192.168.1.8的14941端口转发到172.20.53.2的3389端口上

netsh interface ipv6 install
netsh interface portproxy add v4tov4 listenaddress=192.168.1.8 listenport=14941 connectaddress=172.20.53.2 connectport=3389

取消上面的端口转发

netsh interface portproxy delete v4tov4 listenaddress=192.168.1.8 listenport=14941

查看已经配置端口转发

netsh interface portproxy show v4tov4

2、有线网络

显示网络接口信息

netsh interface show interface

配置 IP 地址

设置静态 IP 地址:

netsh interface ip set address name="Local Area Connection" static 192.168.1.10 255.255.255.0 192.168.1.1

设置DHCP动态 IP 地址:

netsh interface ip set address name="Local Area Connection" source=dhcp

配置 DNS 服务器

设置静态 DNS 服务器:

netsh interface ip set dns name="Local Area Connection" static 8.8.8.8

设置动态 DNS 服务器:

netsh interface ip set dns name="Local Area Connection" source=dhcp

重置网络

netsh winsock reset

3、无线网络

查看无线网络配置文件:

netsh wlan show profiles
#列出所有无线网络接口
netsh wlan show interfaces
#列出所有可用的无线网络
netsh wlan show networks

连接到无线网络:

netsh wlan connect name="SSID"
netsh wlan connect name="SSID" ssid="SSID" interface="无线网络连接"

删除无线网络配置文件:

netsh wlan delete profile name="SSID"

查看wifi密码的命令

netsh wlan show  profile  name ="VIPWIFI"  key=clear

导出配置文件

netsh wlan export profile name="VIPWIFI" folder=D:\

开启无线接口

netsh interface set interface "Interface Name" enabled

断开连接

netsh wlan disconnect

4、管理路由

查看路由表:

netsh interface ip show route

添加静态路由:

netsh interface ip add route 192.168.2.0/24 "Local Area Connection" 192.168.1.1

举例:

netsh interface ip add route 192.168.2.0/24 "以太网" 192.168.1.1

删除静态路由:

netsh interface ip delete route 192.168.2.0/24 "Local Area Connection"

使用示例

假设你想将网络接口 "Ethernet" 的 IP 地址设置为静态的 192.168.1.100,子网掩码为 255.255.255.0,默认网关为 192.168.1.1,并且设置 DNS 服务器为 8.8.8.8。你可以运行以下命令:

netsh interface ip set address name="Ethernet" static 192.168.1.100 255.255.255.0 192.168.1.1

netsh interface ip set dns name="Ethernet" static 8.8.8.8

如果你想通过 DHCP 获取 IP 地址和 DNS 服务器,可以运行:

netsh interface ip set address name="Ethernet" source=dhcp
netsh interface ip set dns name="Ethernet" source=dhcp

5、防火墙

查看防火墙设置

#查看防火墙设置
netsh advfirewall show allprofiles
# 查看防火墙的状态
netsh firewall show state

启用/禁用防火墙

#启用防火墙: 
netsh advfirewall set allprofiles state on
#禁用防火墙: 
netsh advfirewall set allprofiles state off

添加防火墙规则

允许某个程序通过防火墙:

netsh advfirewall firewall add rule name="Allow Program" dir=in action=allow program="C:\path\to\program.exe" enable=yes

删除入站程序规则

netsh advfirewall firewall delete rule name=test dir=in program="C:\path\to\program.exe"

添加入站规则

netsh advfirewall firewall add rule name=test dir=in action=allow protocol=tcp localport=333 

删除入站规则

netsh advfirewall firewall delete rule name=test dir=in protocol=tcp localport=333 

添加出站规则

netsh advfirewall firewall add rule name=test dir=out action=allow protocol=tcp localport=333 

删除出站规则

netsh advfirewall firewall delete rule name=test dir=out protocol=tcp localport=333 

6、基础命令

查看网络配置

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服务器地址。

配置接口IP/网关IP

netsh interface ip set address "本地连接" static 10.1.2.90 255.255.255.0 10.1.2.254 1

配置自动换取IP地址,DNS地址及wins地址

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

导出网络配置文件:

netsh -c interface dump > d:/1.txt
#导出配置脚本
netsh -c interface ip dump > d:/interface.txt

导入配置脚本:

netsh -f c:/interface.txt

7、抓包

netsh抓包

#开启
netsh trace start capture=YES report=YES persistent=YES
#停止
netsh trace stop

指定输出文件路径

netsh trace start capture=yes report=yes persistent=yes tracefile=C:\Logs\nettrace.etl

过滤特定流量: 捕获特定 IP 地址的数据包:

netsh trace start capture=yes ipv4.address=192.168.1.100

捕获的数据可能较大,可通过 maxsize 参数限制文件大小,例如:

netsh trace start capture=yes maxsize=100

Network Monitor 3.4微软抓包软件下载

Download Microsoft Network Monitor 3.4 (archive) from Official Microsoft Download Centeropen in new window

转换文件格式: 将 .etl 文件转换为 Wireshark 支持的 .pcapng 格式:

etl2pcapng.exe input.etl output.pcapng

注意:Microsoft Message Analyzer (MMA) 已停用,其 下载已于 2019 年 11 月 25 日从 microsoft.com 站点删除的软件包。 目前没有 Microsoft 的 Microsoft 替代 消息分析器 此时的发展。对于类似的功能,请考虑使用 第三方网络协议分析器工具,例如 Wireshark

8、网桥

如果您的环境中有两个或多个适配器,并且需要将它们合并为一个 网桥 为了在不同的网段之间共享资源,Netsh 可以轻松创建、修改和删除网桥。

主要使用命令:

列出所有现有的桥梁:

netsh bridge list

查看桥接器可用的适配器:

netsh bridge show adapter

在两个适配器之间创建桥接器(按名称、GUID 或 IfIndex):

netsh bridge create "Wi-Fi" "Ethernet"

向已创建的桥添加适配器:

netsh bridge add "NombreAdaptador" to "GUIDpuente"

从网桥中删除适配器:

netsh bridge remove "NombreAdaptador" from "GUIDpuente"

从桥中删除所有适配器并将其销毁:

netsh bridge destroy "GUIDpuente"

通常最方便的做法是在采取行动之前先使用列表命令识别适配器和桥接器 GUID,以避免错误。

9、WinHTTP 代理设置

在许多企业网络中,互联网连接通过 代理服务器Windows 管理的代理主要有两种类型:用户代理(用于浏览器和应用程序)和 WinHTTP 代理(用于内部服务和系统应用程序)。 的netsh 在 Winhttp 上下文中,它允许您配置、重置和显示此系统代理的设置

要从 Internet Explorer 导入设置:

netsh winhttp import proxy source=ie

要定义新的代理并排除某些域:

netsh winhttp set proxy proxy-server="http=mi-proxy;https=mi-secure-proxy:88" bypass-list="*.empresa.com"

重置设置:

netsh winhttp reset proxy

显示当前设置:

netsh winhttp show proxy

甚至可以使用“set advproxy”以 JSON 格式定义高级代理选项,这在高度控制或自动化的环境中很有用。