Image

知识库 → 在命令行上使用 tshark 实用程序监控 Linux 上的网络

[虚拟服务器]
出版日期: 28.03.2024

要在 Linux Debian 12 操作系统上监控网络,请在命令行上安装 tshark 实用程序 - 这是 WireShark 程序的控制台版本。

1. 安装

apt update
apt install tshark

2. 使用示例

2.1 列出所有接口

tshark -D
1. enp3s0
2. any
3. lo (Loopback)
4. wlp2s0
5. enp1s0
6. bluetooth0
7. bluetooth-monitor
8. nflog
9. nfqueue
10. dbus-system
11. dbus-session
12. ciscodump (Cisco remote capture)
13. dpauxmon (DisplayPort AUX channel monitor capture)
14. randpkt (Random packet generator)
15. sdjournal (systemd Journal Export)
16. sshdump (SSH remote capture)
17. udpdump (UDP Listener remote capture)
18. wifidump (Wi-Fi remote capture)

2.2 监控网络接口

tshark -i enp3s0
Capturing on 'enp3s0'
 ** (tshark:4003) 00:29:16.013682 [Main MESSAGE] -- Capture started.
 ** (tshark:4003) 00:29:16.013782 [Main MESSAGE] -- File: "/tmp/wireshark_enp3s0RQIBL2.pcapng"
    1 0.000000000 192.zzz.1.zzz → 192.xxx.1.xxx SSH 298 Server: Encrypted packet (len=244)
    2 0.002655201 192.zzz.1.zzz → 192.xxx.1.xxx TCP 60 60466 → 22 [ACK] Seq=1 Ack=245 Win=510 Len=0

2.3 监控网络接口UDP协议

tshark -i enp3s0 udp
Capturing on 'enp3s0'
 ** (tshark:4031) 00:31:45.874197 [Main MESSAGE] -- Capture started.
 ** (tshark:4031) 00:31:45.874342 [Main MESSAGE] -- File: "/tmp/wireshark_enp3s0S5KIL2.pcapng"
    1 0.000000000 192.xxx.1.xxx → 239.255.255.250 UDP 122 39749 → 51200 Len=80
    2 0.023937161 192.zzz.1.zzz → 239.255.255.250 SSDP 483 NOTIFY * HTTP/1.1 
    4 0.054497234 192.zzz.1.zzz → 239.255.255.250 SSDP 492 NOTIFY * HTTP/1.1 

2.4 只监控特定IP

tshark -i enp3s0 host 192.zzz.1.zzz

2.5 仅监控特定端口

tshark -nnSX port 443

2.6 HEX模式下特定IP监控

tshark -i enp3s0 -x host 192.zzz.1.zzz
Capturing on 'enp3s0'
 ** (tshark:4111) 00:35:37.357806 [Main MESSAGE] -- Capture started.
 ** (tshark:4111) 00:35:37.357914 [Main MESSAGE] -- File: "/tmp/wireshark_enp3s0LOI7K2.pcapng"
0000  08 d2 3e ef 91 24 68 1d ef 3f 0a 0c 08 00 45 10   ..>..$h..?....E.
0010  01 1c ce 7b 40 00 40 06 e6 f0 c0 a8 01 7e c0 a8   ...{@.@......~..
0020  01 91 00 16 ec 32 43 04 e1 79 16 bc f7 ae 50 18   .....2C..y....P.
0030  01 f5 85 6e 00 00 00 00 00 d0 5a f2 ae a9 d0 18   ...n......Z.....

2.7 日志记录

tshark -w /tmp/tshark-log.pcap -i enp3s0 -x host 192.zzz.1.zzz

2.8 查看日志文件

tshark -r /tmp/tshark-log.pcap 

控制台中有多个用于网络监控的实用程序,例如 netstat、tcpdump、iftop、nload。





暂时没有评论