网络调试--tcpdump

网络调试的手段工具:tcpdumpwireshark

抓包–tcpdump

下载:http://www.tcpdump.org

1
tcpdump -i wlan0 -p  -w file.pcap

常用参数:

arg
-p将网卡设置为非混杂模式
-n对地址以数字方式显式,否则显式为主机名,也就是说-n选项不做主机名解析。
-nn除了-n的作用外,还把端口显示为数值,否则显示端口服务名。
-c指定要抓取的包数量
-v当分析和打印的时候,产生详细的输出
-vv产生比-v更详细的输出
-w将抓包数据输出到文件中而不是标准输出。可以同时配合-G time选项使得输出文件每time秒就自动切换到另一个文件

Install for arm

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

# host

# for libpcap error: configure: error: Neither flex nor lex was found.
#sudo apt install flex bison

PWD=$(pwd)

TCPDUMP="tcpdump-4.9.2"
LIBPCAP="libpcap-1.9.0"

export CC=arm-linux-gnueabihf-gcc

# http://www.tcpdump.org

for software in ${TCPDUMP} ${LIBPCAP}
do
echo "Download $software ..."
echo "wget http://www.tcpdump.org/release/${software}.tar.gz"
wget http://www.tcpdump.org/release/${software}.tar.gz
echo "tar xvf ${software}.tar.gz"
tar xvf ${software}.tar.gz
done

cd ${LIBPCAP}
./configure --host=arm-linux --with-pcap=linux --prefix=${PWD}/out
make; make install
cd -

cd ${TCPDUMP}
./configure --host=arm-linux --with-system-libpcap=${PWD}/../${LIBPCAP}/out/lib --prefix=${PWD}/out
make; make install
cd -

cp ${PWD}/${TCPDUMP}/out/sbin/tcpdump .

示例

-n:直接打印,-w:保存文件

  • 抓取wlan0中的所有数据包

    1
    # tcpdump -i wlan0 -v -n
  • 抓取wlan0中的udp包

    1
    # tcpdump -i wlan0 -v -n udp
  • 抓取wlan中的5个udp包

    1
    # tcpdump -i wlan0 -v -n udp -c 5
  • 指定端口号

    1
    tcpdump -i wlan0 tcp port 7236 -w rrrr.pcap

    分析–wireshark

netstat

1
2
3
4
5
6
# netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.100.3:40964 192.168.100.2:7236 ESTABLISHED
udp 0 0 239.0.0.11:15550 192.168.100.2:* ESTABLISHED
udp 0 0 239.0.0.11:15551 192.168.100.2:1 ESTABLISHED