网络工程师常用命令大全:从电脑排查到设备配置

全面整理网络工程师日常工作中常用的命令,包括Windows/Linux网络排查命令、路由器配置命令、交换机配置命令等,帮助快速定位和解决网络问题

网络工程师常用命令大全:从电脑排查到设备配置

网络工程师在日常工作中需要掌握各种网络排查和配置命令。本文系统整理从电脑端网络排查到路由器和交换机配置的常用命令,帮助你快速定位和解决网络问题。

第一章:Windows 网络排查命令

1.1 基础网络命令

ipconfig - 查看和配置 IP 地址

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 查看所有网络适配器的IP配置
ipconfig

# 查看详细信息
ipconfig /all

# 释放IP地址(DHCP)
ipconfig /release

# 重新获取IP地址(DHCP)
ipconfig /renew

# 刷新DNS缓存
ipconfig /flushdns

# 显示DNS缓存
ipconfig /displaydns

# 示例输出
# IPv4 地址 . . . . . . . . . . . . : 192.168.1.100
# 子网掩码  . . . . . . . . . . . . : 255.255.255.0
# 默认网关 . . . . . . . . . . . . : 192.168.1.1

ping - 测试网络连通性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 基本ping测试
ping 192.168.1.1

# 指定ping次数
ping -n 10 192.168.1.1

# 指定数据包大小
ping -l 1000 192.168.1.1

# 持续ping(直到Ctrl+C停止)
ping -t 192.168.1.1

# 设置超时时间(毫秒)
ping -w 5000 192.168.1.1

# 示例:测试DNS解析
ping www.baidu.com

tracert - 路由跟踪

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 跟踪到目标的路由路径
tracert 192.168.1.1

# 指定最大跳数
tracert -h 30 8.8.8.8

# 不解析主机名(加快速度)
tracert -d 8.8.8.8

# 示例输出
# 1     1 ms     1 ms     1 ms  192.168.1.1
# 2    10 ms    10 ms    10 ms  10.0.0.1
# 3    20 ms    20 ms    20 ms  202.96.209.133

nslookup - DNS 查询

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 查询域名对应的IP地址
nslookup www.baidu.com

# 指定DNS服务器查询
nslookup www.baidu.com 8.8.8.8

# 查询MX记录(邮件服务器)
nslookup -type=MX baidu.com

# 查询所有记录类型
nslookup -type=ANY baidu.com

# 交互模式
nslookup
> set type=A
> www.baidu.com
> server 8.8.8.8
> exit

netstat - 网络连接状态

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 显示所有连接和监听端口
netstat -an

# 显示TCP连接
netstat -an | findstr TCP

# 显示UDP连接
netstat -an | findstr UDP

# 显示进程ID和程序名
netstat -ano

# 显示路由表
netstat -r

# 显示网络接口统计
netstat -e

# 显示协议统计
netstat -s

# 查找特定端口
netstat -ano | findstr :80

arp - ARP 表管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 显示ARP表
arp -a

# 显示ARP表(详细)
arp -a -v

# 添加静态ARP条目
arp -s 192.168.1.100 00-11-22-33-44-55

# 删除ARP条目
arp -d 192.168.1.100

# 清空ARP表
arp -d *

1.2 高级网络命令

route - 路由表管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 显示路由表
route print

# 显示IPv4路由表
route print -4

# 显示IPv6路由表
route print -6

# 添加路由
route add 192.168.2.0 mask 255.255.255.0 192.168.1.1

# 添加默认路由
route add 0.0.0.0 mask 0.0.0.0 192.168.1.1

# 删除路由
route delete 192.168.2.0

# 修改路由
route change 192.168.2.0 mask 255.255.255.0 192.168.1.2

netsh - 网络配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 显示网络接口
netsh interface show interface

# 显示IP配置
netsh interface ip show config

# 设置静态IP
netsh interface ip set address "本地连接" static 192.168.1.100 255.255.255.0 192.168.1.1

# 设置DNS
netsh interface ip set dns "本地连接" static 8.8.8.8

# 显示防火墙状态
netsh advfirewall show allprofiles

# 添加防火墙规则
netsh advfirewall firewall add rule name="允许80端口" dir=in action=allow protocol=TCP localport=80

# 显示WLAN配置
netsh wlan show profiles
netsh wlan show profile name="WiFi名称" key=clear

telnet - 远程连接测试

1
2
3
4
5
6
# 测试端口连通性
telnet 192.168.1.1 80
telnet www.baidu.com 443

# 如果telnet未启用,需要先启用
# 控制面板 -> 程序 -> 启用或关闭Windows功能 -> Telnet客户端

pathping - 路径和延迟分析

1
2
3
4
5
6
7
8
# 路径跟踪和延迟分析
pathping 8.8.8.8

# 不解析主机名
pathping -n 8.8.8.8

# 指定跳数
pathping -h 30 8.8.8.8

第二章:Linux 网络排查命令

2.1 基础网络命令

ifconfig - 网络接口配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# 查看所有网络接口
ifconfig

# 查看特定接口
ifconfig eth0

# 启用网络接口
ifconfig eth0 up

# 禁用网络接口
ifconfig eth0 down

# 配置IP地址
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# 配置IP地址和网关
ifconfig eth0 192.168.1.100 netmask 255.255.255.0
route add default gw 192.168.1.1

# 查看详细信息
ifconfig -a

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
26
27
28
29
30
31
32
33
# 查看所有网络接口
ip addr show
# 或简写
ip a

# 查看路由表
ip route show
# 或简写
ip r

# 添加IP地址
ip addr add 192.168.1.100/24 dev eth0

# 删除IP地址
ip addr del 192.168.1.100/24 dev eth0

# 添加路由
ip route add 192.168.2.0/24 via 192.168.1.1

# 添加默认路由
ip route add default via 192.168.1.1

# 删除路由
ip route del 192.168.2.0/24

# 查看ARP表
ip neigh show

# 添加ARP条目
ip neigh add 192.168.1.100 lladdr 00:11:22:33:44:55 dev eth0

# 查看网络统计
ip -s link show eth0

ping - 网络连通性测试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 基本ping测试
ping 192.168.1.1

# 指定ping次数
ping -c 10 192.168.1.1

# 指定数据包大小
ping -s 1000 192.168.1.1

# 设置时间间隔(秒)
ping -i 2 192.168.1.1

# 设置超时时间(秒)
ping -W 5 192.168.1.1

# 持续ping
ping 192.168.1.1
# 按Ctrl+C停止

traceroute - 路由跟踪

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 跟踪路由路径
traceroute 8.8.8.8

# 指定最大跳数
traceroute -m 30 8.8.8.8

# 不解析主机名
traceroute -n 8.8.8.8

# 指定数据包大小
traceroute -s 1000 8.8.8.8

# 使用UDP(默认)
traceroute -U 8.8.8.8

# 使用ICMP
traceroute -I 8.8.8.8

# 使用TCP
traceroute -T 8.8.8.8

dig - DNS 查询工具

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 查询A记录
dig www.baidu.com

# 查询特定记录类型
dig www.baidu.com A
dig baidu.com MX
dig baidu.com NS

# 指定DNS服务器
dig @8.8.8.8 www.baidu.com

# 简短输出
dig +short www.baidu.com

# 反向DNS查询
dig -x 8.8.8.8

# 显示所有记录
dig www.baidu.com ANY

nslookup - DNS 查询

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 基本查询
nslookup www.baidu.com

# 指定DNS服务器
nslookup www.baidu.com 8.8.8.8

# 查询MX记录
nslookup -type=MX baidu.com

# 交互模式
nslookup
> set type=A
> www.baidu.com
> server 8.8.8.8
> exit

netstat - 网络连接状态

 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
# 显示所有连接
netstat -a

# 显示TCP连接
netstat -at

# 显示UDP连接
netstat -au

# 显示监听端口
netstat -l

# 显示进程和程序名
netstat -p

# 显示数字地址(不解析主机名)
netstat -n

# 组合使用
netstat -tulpn

# 显示路由表
netstat -r

# 显示网络接口统计
netstat -i

ss - 现代 socket 统计工具

 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
# 显示所有连接
ss -a

# 显示TCP连接
ss -t

# 显示UDP连接
ss -u

# 显示监听端口
ss -l

# 显示进程信息
ss -p

# 显示数字地址
ss -n

# 组合使用
ss -tulpn

# 显示特定端口
ss -tulpn | grep :80

# 显示特定状态的连接
ss -tan state established

arp - ARP 表管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 显示ARP表
arp -a

# 显示特定接口的ARP表
arp -a -i eth0

# 添加静态ARP条目
arp -s 192.168.1.100 00:11:22:33:44:55

# 删除ARP条目
arp -d 192.168.1.100

# 清空ARP表
ip -s -s neigh flush all

2.2 高级网络命令

tcpdump - 网络抓包

 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
# 抓取所有数据包
tcpdump

# 抓取特定接口的数据包
tcpdump -i eth0

# 抓取特定主机的数据包
tcpdump host 192.168.1.100

# 抓取特定端口的数据包
tcpdump port 80

# 抓取TCP数据包
tcpdump tcp

# 抓取UDP数据包
tcpdump udp

# 保存到文件
tcpdump -w capture.pcap

# 从文件读取
tcpdump -r capture.pcap

# 显示详细信息
tcpdump -v

# 显示更详细信息
tcpdump -vv

# 不解析主机名
tcpdump -n

# 组合条件
tcpdump -i eth0 host 192.168.1.100 and port 80

wireshark/tshark - 图形化抓包工具

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 使用tshark(命令行版wireshark)
tshark -i eth0

# 抓取特定主机
tshark -i eth0 host 192.168.1.100

# 保存到文件
tshark -i eth0 -w capture.pcap

# 从文件读取
tshark -r capture.pcap

# 显示特定字段
tshark -r capture.pcap -T fields -e ip.src -e ip.dst

iptables - 防火墙规则

 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
# 查看所有规则
iptables -L -n -v

# 查看NAT规则
iptables -t nat -L -n -v

# 允许特定端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 拒绝特定IP
iptables -A INPUT -s 192.168.1.100 -j DROP

# 允许特定IP
iptables -A INPUT -s 192.168.1.100 -j ACCEPT

# 删除规则
iptables -D INPUT 1

# 清空所有规则
iptables -F

# 保存规则
iptables-save > /etc/iptables/rules.v4

# 恢复规则
iptables-restore < /etc/iptables/rules.v4

route - 路由表管理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 查看路由表
route -n

# 添加路由
route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

# 添加默认路由
route add default gw 192.168.1.1

# 删除路由
route del -net 192.168.2.0 netmask 255.255.255.0

# 删除默认路由
route del default

mtr - 网络诊断工具

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 网络诊断(结合ping和traceroute)
mtr 8.8.8.8

# 报告模式(运行指定次数后退出)
mtr -r -c 10 8.8.8.8

# 不解析主机名
mtr -n 8.8.8.8

# 指定数据包大小
mtr -s 1000 8.8.8.8

第三章:路由器常用命令

3.1 Cisco 路由器命令

基础配置命令

 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
# 进入特权模式
enable
# 或简写
en

# 进入全局配置模式
configure terminal
# 或简写
conf t

# 配置主机名
hostname Router1

# 配置接口IP地址
interface gigabitethernet 0/0
ip address 192.168.1.1 255.255.255.0
no shutdown
exit

# 配置默认路由
ip route 0.0.0.0 0.0.0.0 192.168.1.254

# 配置静态路由
ip route 192.168.2.0 255.255.255.0 192.168.1.2

# 保存配置
write memory
# 或
copy running-config startup-config
# 或简写
wr

查看和诊断命令

 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
47
48
49
50
51
52
53
54
55
56
# 查看运行配置
show running-config
# 或简写
sh run

# 查看启动配置
show startup-config
# 或简写
sh start

# 查看接口状态
show interfaces
# 或简写
sh int

# 查看特定接口
show interface gigabitethernet 0/0
# 或简写
sh int g0/0

# 查看IP接口摘要
show ip interface brief
# 或简写
sh ip int br

# 查看路由表
show ip route
# 或简写
sh ip ro

# 查看ARP表
show arp
# 或简写
sh arp

# 查看IP协议信息
show ip protocols

# 查看CDP邻居
show cdp neighbors
# 或简写
sh cdp nei

# 查看CDP邻居详细信息
show cdp neighbors detail

# 查看版本信息
show version
# 或简写
sh ver

# 查看时钟
show clock

# 查看日志
show logging

路由协议配置

 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
# 配置RIP
router rip
version 2
network 192.168.1.0
network 192.168.2.0
no auto-summary
exit

# 配置OSPF
router ospf 1
network 192.168.1.0 0.0.0.255 area 0
network 192.168.2.0 0.0.0.255 area 0
exit

# 配置EIGRP
router eigrp 100
network 192.168.1.0
network 192.168.2.0
no auto-summary
exit

# 配置BGP
router bgp 65000
neighbor 192.168.1.2 remote-as 65001
network 192.168.3.0 mask 255.255.255.0
exit

ACL 配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 配置标准ACL
access-list 10 permit 192.168.1.0 0.0.0.255
access-list 10 deny any

# 配置扩展ACL
access-list 100 permit tcp 192.168.1.0 0.0.0.255 any eq 80
access-list 100 permit tcp 192.168.1.0 0.0.0.255 any eq 443
access-list 100 deny ip any any

# 应用ACL到接口
interface gigabitethernet 0/0
ip access-group 10 in
# 或
ip access-group 100 out
exit

NAT 配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 配置静态NAT
ip nat inside source static 192.168.1.100 202.96.209.100

# 配置动态NAT
ip nat inside source list 1 interface gigabitethernet 0/1 overload
access-list 1 permit 192.168.1.0 0.0.0.255

# 配置接口
interface gigabitethernet 0/0
ip nat inside
exit

interface gigabitethernet 0/1
ip nat outside
exit

3.2 华为路由器命令

基础配置命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 进入系统视图
system-view
# 或简写
sys

# 配置主机名
sysname Router1

# 配置接口IP地址
interface GigabitEthernet0/0/0
ip address 192.168.1.1 255.255.255.0
undo shutdown
quit

# 配置默认路由
ip route-static 0.0.0.0 0.0.0.0 192.168.1.254

# 配置静态路由
ip route-static 192.168.2.0 255.255.255.0 192.168.1.2

# 保存配置
save

查看和诊断命令

 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
# 查看当前配置
display current-configuration
# 或简写
dis cur

# 查看接口信息
display interface GigabitEthernet0/0/0
# 或简写
dis int g0/0/0

# 查看接口摘要
display ip interface brief
# 或简写
dis ip int br

# 查看路由表
display ip routing-table
# 或简写
dis ip ro

# 查看ARP表
display arp
# 或简写
dis arp

# 查看版本信息
display version
# 或简写
dis ver

# 查看设备信息
display device
# 或简写
dis dev

路由协议配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 配置OSPF
ospf 1
area 0
network 192.168.1.0 0.0.0.255
network 192.168.2.0 0.0.0.255
quit
quit

# 配置BGP
bgp 65000
peer 192.168.1.2 as-number 65001
network 192.168.3.0 255.255.255.0
quit

ACL 配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 配置基本ACL
acl number 2000
rule 5 permit source 192.168.1.0 0.0.0.255
rule 10 deny
quit

# 配置高级ACL
acl number 3000
rule 5 permit tcp source 192.168.1.0 0.0.0.255 destination-port eq 80
rule 10 permit tcp source 192.168.1.0 0.0.0.255 destination-port eq 443
rule 15 deny ip
quit

# 应用ACL到接口
interface GigabitEthernet0/0/0
traffic-filter inbound acl 2000
# 或
traffic-filter outbound acl 3000
quit

第四章:交换机常用命令

4.1 Cisco 交换机命令

基础配置命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 进入特权模式
enable

# 进入全局配置模式
configure terminal

# 配置主机名
hostname Switch1

# 配置管理IP地址
interface vlan 1
ip address 192.168.1.10 255.255.255.0
no shutdown
exit

# 配置默认网关
ip default-gateway 192.168.1.1

# 保存配置
write memory

VLAN 配置

 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
# 创建VLAN
vlan 10
name Sales
exit

vlan 20
name IT
exit

# 将接口加入VLAN(接入模式)
interface gigabitethernet 0/1
switchport mode access
switchport access vlan 10
exit

# 配置Trunk接口
interface gigabitethernet 0/24
switchport mode trunk
switchport trunk allowed vlan 10,20,30
exit

# 查看VLAN信息
show vlan
# 或简写
sh vlan

# 查看VLAN简要信息
show vlan brief
# 或简写
sh vlan br

查看和诊断命令

 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
# 查看MAC地址表
show mac address-table
# 或简写
sh mac add

# 查看接口状态
show interfaces
# 或简写
sh int

# 查看接口摘要
show interfaces status
# 或简写
sh int status

# 查看端口安全
show port-security
# 或简写
sh port-sec

# 查看生成树
show spanning-tree
# 或简写
sh spanning-tree

# 查看CDP邻居
show cdp neighbors
# 或简写
sh cdp nei

STP 配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 配置根桥
spanning-tree vlan 10 root primary

# 配置次根桥
spanning-tree vlan 10 root secondary

# 配置端口优先级
interface gigabitethernet 0/1
spanning-tree port-priority 16
exit

# 配置端口成本
interface gigabitethernet 0/1
spanning-tree cost 19
exit

# 启用快速端口
interface gigabitethernet 0/1
spanning-tree portfast
exit

端口安全配置

1
2
3
4
5
6
7
8
# 启用端口安全
interface gigabitethernet 0/1
switchport mode access
switchport port-security
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security mac-address sticky
exit

4.2 华为交换机命令

基础配置命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 进入系统视图
system-view

# 配置主机名
sysname Switch1

# 配置管理IP地址
interface Vlanif1
ip address 192.168.1.10 255.255.255.0
quit

# 配置默认网关
ip route-static 0.0.0.0 0.0.0.0 192.168.1.1

# 保存配置
save

VLAN 配置

 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
# 创建VLAN
vlan 10
description Sales
quit

vlan 20
description IT
quit

# 将接口加入VLAN(接入模式)
interface GigabitEthernet0/0/1
port link-type access
port default vlan 10
quit

# 配置Trunk接口
interface GigabitEthernet0/0/24
port link-type trunk
port trunk allow-pass vlan 10 20 30
quit

# 查看VLAN信息
display vlan
# 或简写
dis vlan

查看和诊断命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 查看MAC地址表
display mac-address
# 或简写
dis mac-add

# 查看接口信息
display interface GigabitEthernet0/0/1
# 或简写
dis int g0/0/1

# 查看接口摘要
display interface brief
# 或简写
dis int br

# 查看生成树
display stp
# 或简写
dis stp

# 查看LLDP邻居
display lldp neighbor
# 或简写
dis lldp nei

STP 配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 配置根桥
stp root primary

# 配置次根桥
stp root secondary

# 配置端口优先级
interface GigabitEthernet0/0/1
stp port priority 16
quit

# 配置端口成本
interface GigabitEthernet0/0/1
stp cost 19
quit

第五章:常用网络排查流程

5.1 网络不通排查流程

步骤一:检查本地配置

1
2
3
4
5
6
7
# Windows
ipconfig /all
ping 127.0.0.1

# Linux
ip addr show
ping 127.0.0.1

步骤二:检查网关连通性

1
2
3
4
5
# Windows
ping 192.168.1.1

# Linux
ping 192.168.1.1

步骤三:检查 DNS 解析

1
2
3
4
5
6
7
# Windows
nslookup www.baidu.com
ping www.baidu.com

# Linux
dig www.baidu.com
ping www.baidu.com

步骤四:检查路由

1
2
3
4
5
6
7
# Windows
route print
tracert 8.8.8.8

# Linux
ip route show
traceroute 8.8.8.8

步骤五:检查 ARP 表

1
2
3
4
5
6
7
# Windows
arp -a

# Linux
arp -a
# 或
ip neigh show

5.2 端口不通排查流程

步骤一:检查服务是否运行

1
2
3
4
5
6
7
8
9
# Windows
netstat -ano | findstr :80
tasklist | findstr PID

# Linux
netstat -tulpn | grep :80
# 或
ss -tulpn | grep :80
ps aux | grep PID

步骤二:检查防火墙

1
2
3
4
5
# Windows
netsh advfirewall show allprofiles

# Linux
iptables -L -n -v

步骤三:测试端口连通性

1
2
3
4
5
6
7
8
9
# Windows
telnet 192.168.1.100 80
# 或使用PowerShell
Test-NetConnection -ComputerName 192.168.1.100 -Port 80

# Linux
telnet 192.168.1.100 80
# 或
nc -zv 192.168.1.100 80

5.3 网络慢排查流程

步骤一:测试延迟

1
2
3
4
5
6
7
# Windows
ping -n 100 192.168.1.1
pathping 8.8.8.8

# Linux
ping -c 100 192.168.1.1
mtr 8.8.8.8

步骤二:检查带宽使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Windows
netsh interface show interface
# 使用任务管理器查看网络使用

# Linux
iftop -i eth0
# 或
nethogs
# 或
vnstat

步骤三:检查网络连接数

1
2
3
4
5
6
7
# Windows
netstat -an | find /c "ESTABLISHED"

# Linux
netstat -an | grep ESTABLISHED | wc -l
# 或
ss -s

第六章:实用脚本和工具

6.1 Windows 批处理脚本

网络诊断脚本

 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
@echo off
echo ===== 网络诊断脚本 =====
echo.

echo [1] 检查IP配置
ipconfig /all
echo.

echo [2] 测试网关连通性
ping -n 4 192.168.1.1
echo.

echo [3] 测试DNS解析
nslookup www.baidu.com
echo.

echo [4] 显示路由表
route print
echo.

echo [5] 显示ARP表
arp -a
echo.

echo [6] 显示网络连接
netstat -an | findstr ESTABLISHED
echo.

pause

端口扫描脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@echo off
set target=192.168.1.100
set ports=80,443,22,3389,3306

echo 扫描 %target% 的端口: %ports%
for %%p in (%ports%) do (
    echo 测试端口 %%p...
    telnet %target% %%p
)
pause

6.2 Linux Shell 脚本

网络诊断脚本

 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
#!/bin/bash

echo "===== 网络诊断脚本 ====="
echo

echo "[1] 检查IP配置"
ip addr show
echo

echo "[2] 测试网关连通性"
ping -c 4 192.168.1.1
echo

echo "[3] 测试DNS解析"
dig www.baidu.com +short
echo

echo "[4] 显示路由表"
ip route show
echo

echo "[5] 显示ARP表"
ip neigh show
echo

echo "[6] 显示网络连接"
ss -tulpn
echo

端口扫描脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/bash

target=$1
ports=(80 443 22 3389 3306)

if [ -z "$target" ]; then
    echo "用法: $0 <目标IP>"
    exit 1
fi

echo "扫描 $target 的端口..."
for port in "${ports[@]}"; do
    if nc -zv -w 1 $target $port 2>&1 | grep -q "succeeded"; then
        echo "端口 $port: 开放"
    else
        echo "端口 $port: 关闭"
    fi
done

结语:掌握命令,提升效率

网络工程师需要:

  1. 熟练掌握基础命令:ping、tracert、netstat 等
  2. 理解网络原理:理解命令背后的网络原理
  3. 建立排查流程:形成系统化的排查思路
  4. 持续学习:关注新技术和新工具

记住:

  • 命令是工具:理解原理比记住命令更重要
  • 实践出真知:多动手实践,积累经验
  • 文档是朋友:遇到问题查文档
  • 脚本提高效率:编写脚本自动化重复工作

愿每一位网络工程师都能熟练掌握这些命令,快速定位和解决网络问题!

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计