Skip to content

网络命令

网络配置

ipconfig - IP 配置

cmd
ipconfig                          # 显示 IP 配置
ipconfig /all                     # 显示详细信息
ipconfig /release                 # 释放 IP
ipconfig /renew                   # 更新 IP
ipconfig /flushdns                # 清除 DNS 缓存
ipconfig /displaydns              # 显示 DNS 缓存
ipconfig /registerdns             # 注册 DNS
ipconfig /showclassid adapter     # 显示 DHCP 类 ID
ipconfig /setclassid adapter classid  # 设置 DHCP 类 ID

netsh - 网络配置

接口配置

cmd
netsh interface show interface    # 显示接口
netsh interface ip show config    # 显示 IP 配置
netsh interface ip show address   # 显示地址
netsh interface ip show dns       # 显示 DNS
netsh interface ip set address "Ethernet0" static 192.168.1.100 255.255.255.0 192.168.1.1
netsh interface ip set address "Ethernet0" dhcp
netsh interface ip set dns "Ethernet0" static 8.8.8.8
netsh interface ip add dns "Ethernet0" 8.8.4.4 index=2
netsh interface ip set dns "Ethernet0" dhcp

防火墙配置

cmd
netsh advfirewall show allprofiles  # 显示所有配置文件
netsh advfirewall set allprofiles state on  # 启用防火墙
netsh advfirewall set allprofiles state off # 禁用防火墙
netsh advfirewall firewall add rule name="Allow Port 80" dir=in action=allow protocol=tcp localport=80
netsh advfirewall firewall add rule name="Allow Port 443" dir=in action=allow protocol=tcp localport=443
netsh advfirewall firewall add rule name="Allow App" dir=in action=allow program="C:\app\app.exe"
netsh advfirewall firewall delete rule name="Allow Port 80"
netsh advfirewall firewall show rule name=all
netsh advfirewall export "firewall.wfw"  # 导出规则
netsh advfirewall import "firewall.wfw"  # 导入规则

网络重置

cmd
netsh winsock reset               # 重置 Winsock
netsh int ip reset                # 重置 IP 协议栈
netsh interface tcp reset         # 重置 TCP/IP

网络诊断

ping - 测试连通性

cmd
ping 192.168.1.1                  # 测试连通性
ping -n 5 192.168.1.1             # 发送5个包
ping -t 192.168.1.1               # 持续 ping
ping -l 1000 192.168.1.1          # 指定包大小
ping -a 192.168.1.1               # 解析主机名
ping -4 192.168.1.1               # 使用 IPv4
ping -6 2001:db8::1               # 使用 IPv6
ping -w 5000 192.168.1.1          # 超时时间5秒

tracert - 路由追踪

cmd
tracert 8.8.8.8                   # 追踪路由
tracert -d 8.8.8.8                # 不解析主机名
tracert -h 20 8.8.8.8             # 最大跳数
tracert -w 5000 8.8.8.8           # 超时时间
tracert -4 8.8.8.8                # 使用 IPv4
tracert -6 2001:db8::1            # 使用 IPv6

pathping - 路径分析

cmd
pathping 8.8.8.8                  # 路径分析
pathping -n 8.8.8.8               # 不解析主机名
pathping -h 20 8.8.8.8            # 最大跳数
pathping -p 1000 8.8.8.8          # ping 间隔
pathping -q 10 8.8.8.8            # 每跳查询次数

nslookup - DNS 查询

cmd
nslookup example.com              # 查询域名
nslookup example.com 8.8.8.8      # 指定 DNS 服务器
nslookup -type=A example.com      # A 记录
nslookup -type=AAAA example.com   # AAAA 记录
nslookup -type=MX example.com     # MX 记录
nslookup -type=NS example.com     # NS 记录
nslookup -type=TXT example.com    # TXT 记录
nslookup -type=CNAME example.com  # CNAME 记录
nslookup -type=SOA example.com    # SOA 记录

交互模式:

cmd
nslookup
> server 8.8.8.8
> set type=mx
> example.com
> exit

网络连接

netstat - 网络统计

cmd
netstat                           # 显示连接
netstat -a                        # 显示所有连接
netstat -n                        # 数字形式显示
netstat -o                        # 显示 PID
netstat -b                        # 显示程序名
netstat -r                        # 显示路由表
netstat -s                        # 显示统计
netstat -e                        # 显示以太网统计
netstat -p tcp                    # 只显示 TCP
netstat -p udp                    # 只显示 UDP
netstat -an                       # 组合使用
netstat -ano                      # 常用组合
netstat -ano | findstr :80        # 查找端口
netstat -ano | findstr LISTENING  # 查找监听端口

查看端口占用

cmd
netstat -ano | findstr :80        # 查找80端口
netstat -ano | findstr :443       # 查找443端口
for /f "tokens=5" %a in ('netstat -ano ^| findstr :80') do tasklist /fi "pid eq %a"

route - 路由表

cmd
route print                       # 显示路由表
route print -4                    # IPv4 路由
route print -6                    # IPv6 路由
route add 10.0.0.0 mask 255.0.0.0 192.168.1.1  # 添加路由
route add 10.0.0.0 mask 255.0.0.0 192.168.1.1 -p  # 永久路由
route delete 10.0.0.0             # 删除路由
route change 10.0.0.0 mask 255.0.0.0 192.168.1.2  # 修改路由

arp - ARP 表

cmd
arp -a                            # 显示 ARP 表
arp -a 192.168.1.1                # 指定接口
arp -d                            # 清除 ARP 表
arp -d 192.168.1.1                # 删除指定条目
arp -s 192.168.1.1 00-11-22-33-44-55  # 添加静态条目
netsh interface ip delete arpcache  # 清除 ARP 缓存

网络共享

net share - 共享管理

cmd
net share                         # 列出共享
net share sharename               # 查看共享信息
net share sharename=C:\folder     # 创建共享
net share sharename=C:\folder /grant:everyone,full
net share sharename /delete       # 删除共享
net share sharename /users:10     # 限制用户数
net share sharename /remark:"描述"

net use - 网络驱动器

cmd
net use                           # 列出连接
net use Z: \\server\share         # 映射驱动器
net use Z: \\server\share /user:domain\user password
net use Z: /delete                # 删除映射
net use * /delete                 # 删除所有映射
net use Z: /persistent:yes        # 永久映射
net use Z: /persistent:no         # 临时映射

net session - 会话管理

cmd
net session                       # 列出会话
net session \\computer            # 查看指定计算机
net session \\computer /delete    # 断开会话
net session /delete               # 断开所有会话

net file - 打开文件

cmd
net file                          # 列出打开的文件
net file id                       # 查看指定文件
net file id /close                # 关闭文件

远程连接

mstsc - 远程桌面

cmd
mstsc                             # 打开远程桌面
mstsc /v:192.168.1.1              # 连接到指定计算机
mstsc /v:192.168.1.1 /admin       # 管理会话
mstsc /v:192.168.1.1 /f           # 全屏模式
mstsc /v:192.168.1.1 /w:1024 /h:768  # 指定分辨率
mstsc /edit connection.rdp        # 编辑 RDP 文件

shutdown - 远程关机

cmd
shutdown /s                       # 关机
shutdown /r                       # 重启
shutdown /l                       # 注销
shutdown /s /t 60                 # 60秒后关机
shutdown /r /t 60                 # 60秒后重启
shutdown /a                       # 取消关机
shutdown /s /m \\computer         # 远程关机
shutdown /r /m \\computer         # 远程重启
shutdown /s /m \\computer /t 0    # 立即远程关机
shutdown /i                       # 图形界面

网络工具

curl - 数据传输

cmd
curl https://example.com          # GET 请求
curl -I https://example.com       # 只获取头部
curl -X POST https://example.com  # POST 请求
curl -d "name=value" https://example.com
curl -H "Content-Type: application/json" https://example.com
curl -u user:pass https://example.com
curl -o file.txt https://example.com/file
curl -O https://example.com/file.txt
curl -L https://example.com       # 跟随重定向
curl -k https://example.com       # 忽略证书

wget - 文件下载

cmd
wget https://example.com/file.txt
wget -O newname.txt https://example.com/file.txt
wget -c https://example.com/file.txt  # 断点续传
wget -r https://example.com/      # 递归下载
wget --limit-rate=100k https://example.com/file.txt

网络故障排查

常用诊断流程

cmd
ipconfig /all                     # 检查 IP 配置
ping 127.0.0.1                    # 测试本地回环
ping 192.168.1.1                  # 测试网关
ping 8.8.8.8                      # 测试外网 IP
ping google.com                   # 测试 DNS
tracert 8.8.8.8                   # 追踪路由
nslookup google.com               # 检查 DNS
netstat -an                       # 检查端口

重置网络

cmd
netsh winsock reset               # 重置 Winsock
netsh int ip reset                # 重置 IP 协议栈
ipconfig /flushdns                # 清除 DNS 缓存
ipconfig /release                 # 释放 IP
ipconfig /renew                   # 更新 IP

检查防火墙

cmd
netsh advfirewall show allprofiles
netsh advfirewall set allprofiles state off
netsh advfirewall set allprofiles state on

网络监控

持续监控

cmd
:monitor
cls
netstat -an | findstr ESTABLISHED
timeout /t 5 >nul
goto monitor

性能监控

cmd
typeperf "\Network Interface(*)\Bytes Total/sec"
typeperf "\Network Interface(*)\Current Bandwidth"
typeperf "\TCP\Connections Established"
typeperf "\TCP\Segments Received/sec"
typeperf "\TCP\Segments Sent/sec"

下一步学习