[命令] Linux 命令 tcpdump (网络抓包)

内容一:tcpdump 命令格式

# tcpdump <option> <proto> <dir> <type>

内容二:tcpdump 命令选项 (option)

1) -A use ASCII print all data, it is convenient for read website date.
2) -c <number>, number of packet captures
3) -C <file size>,size of file
4) -e, display data link layer
5) -G <second>, create a new file after these seconds 
6) -l, send the output to next command, example: tcpdump -i eth0 -s0 | grep 'Server:'
7) -n, do not resolve domain only display IP address
8) -nn, do not resolve domain and port only display IP address
9) -i <interface>, such as internet card and can use: -i any
10) -p, only display the data which point it self
11) -r <file>, read data from file 
12) -s <snaplen>, size of length of the network package and can use: -s 0 to ulimite the length, the default value is 96B
13) -v , display detail
14) -vv, display detail more than -v
15) -vvv, display detail more than -vv
16) -w <file>, write the output result into a file

内容三:tcpdump 命令协议 (proto)

1) tcp or proto 6
2) udp or proto 17
3) icmp, example: proto \\icmp is icmp
4) ip
5) ip6
6) arp
7) rarp
8) ether
9) wlan

内容四:tcpdump 命令方向 (dir)

1) src
2) dst

内容五:tcpdump 命令种类 (type)

1) host <IP address>
2) net <network segment>, example: 10 is 10.x.x.x/8 , 192.168.0 is 192.168.0.0/24
3) port <port>
4) portrange <port range>

内容六:tcpdump 命令逻辑

1) and or &&
2) or or ||
3) not or !

内容七:tcpdump 命令的使用案例

7.1 显示发送到某个 IP 地址的某个端口的网络数据包

# tcpdump -i any dst 192.168.0.1 and udp port 514

(补充:这里以显示发送到 IP 地址 192.168.0.1 的 UDP 514 端口为例,如果有这类网络数据包的话则详细信息会在命令输出后显示出来)

7.2 显示从某个 IP 地址发送来的网络数据包

# tcpdump -vv src host 192.168.0.1

(补充:这里以显示从 IP 地址 192.168.0.1 发送来的网络数据包为例,如果有这类网络数据包的话则详细信息会在命令输出后显示出来)

7.3 将所有抓到的网络数据包导入到某个文件里

# tcpdump -i any -w /tmp/telnet.cap

(补充:这里以将所有抓到的数据包导出到文件 /tmp/telnet.cap 为例)

7.4 将某个网卡抓到的网络数据包导入到某个文件里

# tcpdump -i eth0 -s 0 -w /tmp/$(hostname)-$(date +"%Y-%m-%d-%H-%M-%S").pcap host 192.168.1.1 or host 192.168.1.2

(补充:这里以将网卡 eth0 网卡上抓到的来源或目的是 192.168.1.1 或 192.168.1.2 的数据包导出到文件 /tmp/$(hostname)-$(date +”%Y-%m-%d-%H-%M-%S”).pcap 为例)