Linux网络调试工具:curl与抓包实战

Linux网络调试工具:curl与抓包实战

本文介绍Linux中两个常用的网络调试工具:curl(HTTP请求工具)和tcpdump(抓包分析),助你快速定位网络问题。


一、curl命令:强大的HTTP客户端

curl是一个命令行工具,用于发送各种网络请求,支持HTTP、HTTPS、FTP等多种协议。

1.1 常见选项

选项 说明
-X 指定HTTP请求方法(GET/POST/PUT/DELETE等)
-H 添加请求头
-d 指定请求数据
-v 显示详细的请求和响应信息
-o 将输出保存到文件
-O 将输出保存到文件,使用远程文件名
-I 只显示响应头
-L 跟随重定向

1.2 发送GET请求

bash 复制代码
# 基本GET请求
curl http://example.com

# 显示详细信息
curl http://127.0.0.1:49800/signature -v

# 只显示响应头
curl -I http://example.com

1.3 发送POST请求

发送JSON数据(数据在命令行中):

bash 复制代码
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"model_id": "job-firede-fe775241-5c8d"}' \
  http://192.168.14.212:5080/omstream/decode_frames/get_messagequeue

发送JSON数据(数据在文件中):

bash 复制代码
curl -X POST \
  -H "Content-Type: application/json" \
  -H "referer:http://192.168.1.180" \
  http://192.168.1.180:5080/streamDecoderService/start \
  -d @start.json

注意 :使用 @文件名 可以从文件读取数据。

1.4 自定义请求头

bash 复制代码
# 添加多个请求头
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer token123" \
  -H "referer:http://192.168.1.180" \
  http://example.com/api

1.5 其他常用操作

下载文件:

bash 复制代码
curl -O http://example.com/file.zip        # 使用原文件名
curl -o myfile.zip http://example.com/file.zip  # 指定文件名

跟随重定向:

bash 复制代码
curl -L http://example.com

显示响应时间:

bash 复制代码
curl -w "Time: %{time_total}s\n" http://example.com

二、tcpdump:网络抓包工具

tcpdump是Linux下强大的网络抓包工具,可以将网络中传输的数据包抓取下来进行分析。

2.1 基本语法

bash 复制代码
tcpdump [选项] [过滤表达式]

2.2 常用选项

选项 说明
-i 指定网卡(any 表示所有网卡)
-w 将抓包结果保存到文件
-r 从文件读取抓包数据
-s 设置抓取长度(0 表示不截断)
-n 不解析主机名和端口名
-nn 不解析主机名和端口名,显示数字
-v 显示详细信息
-c 指定抓包数量

2.3 抓包并保存

bash 复制代码
# 抓取所有数据包并保存
tcpdump -i any -s 0 -w capture.pcap

# 抓取指定IP和端口的数据包
tcpdump -i any host 192.168.14.204 and port 8080 -s 0 -w a.pcap

参数说明

  • -s 0:不截断数据,抓取完整数据包
  • -w:写入文件(.pcap格式可用Wireshark打开)

2.4 过滤表达式

按主机过滤:

bash 复制代码
tcpdump -i eth0 host 192.168.14.209

按端口过滤:

bash 复制代码
tcpdump -i eth0 tcp port 30506
tcpdump -i eth0 port 80

按协议过滤:

bash 复制代码
tcpdump -i eth0 tcp      # TCP协议
tcpdump -i eth0 udp      # UDP协议
tcpdump -i eth0 icmp     # ICMP协议

组合过滤:

bash 复制代码
# 指定IP和端口
tcpdump -i any host 192.168.14.204 and port 8080 -s 0 -w a.pcap

# 源IP和目的端口
tcpdump -i eth0 src host 192.168.1.100 and dst port 80

# 排除某些流量
tcpdump -i eth0 not port 22

2.5 实时查看

bash 复制代码
# 不保存,直接查看
tcpdump -i eth0 -nn

# 查看详细内容
tcpdump -i eth0 -nn -v

# 查看数据包内容(十六进制和ASCII)
tcpdump -i eth0 -nn -X

2.6 读取抓包文件

bash 复制代码
tcpdump -r capture.pcap
tcpdump -r capture.pcap -nn    # 不解析主机名

三、Wireshark分析

抓包保存为 .pcap 文件后,可以使用Wireshark进行图形化分析:

  1. 打开Wireshark
  2. 文件 → 打开 → 选择 .pcap 文件
  3. 使用过滤器分析:
    • http - HTTP流量
    • tcp.port == 80 - 80端口TCP流量
    • ip.addr == 192.168.1.1 - 指定IP的流量

四、实战案例

案例1:调试API接口

bash 复制代码
# 发送请求并查看详细信息
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"action": "test"}' \
  http://api.example.com/test -v

案例2:抓取HTTP流量分析

bash 复制代码
# 抓包
tcpdump -i any host api.example.com and port 80 -s 0 -w http.pcap

# 发送请求
curl http://api.example.com/test

# 用Wireshark分析 http.pcap

案例3:排查网络连接问题

bash 复制代码
# 抓取所有流量
tcpdump -i any host 192.168.14.204 -s 0 -w debug.pcap

# 然后执行你的程序或操作
# ...

# 分析抓包文件
tcpdump -r debug.pcap -nn

五、常用命令速查

curl速查

bash 复制代码
# GET请求
curl http://example.com

# POST JSON
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com

# POST文件
curl -X POST -H "Content-Type: application/json" -d @data.json http://example.com

# 显示详细信息
curl -v http://example.com

tcpdump速查

bash 复制代码
# 抓所有包
tcpdump -i any -s 0 -w all.pcap

# 抓指定IP
tcpdump -i eth0 host 192.168.1.100

# 抓指定端口
tcpdump -i eth0 port 80

# 抓指定IP和端口
tcpdump -i any host 192.168.1.100 and port 8080 -s 0 -w capture.pcap

参考资源

相关推荐
lengjingzju13 小时前
编译与调试完全指南—第10章 网络分析工具
linux
星野川崎20613 小时前
电商多店运维:云机24小时挂机频繁掉线、账号无故风控深度原因分析及解决方案
大数据·运维·服务器·云计算·电商
Logintern0913 小时前
网络通讯协议—TCP/IP协议
网络·网络协议·tcp/ip
ltl14 小时前
新硬件对存储的影响:ZNS、CXL 与计算存储
linux
RisunJan14 小时前
Linux命令-xauth(X11 认证授权管理)
linux·服务器·microsoft
熊IT15 小时前
什么是原生住宅 IP?如何判断一个 IP 是否真正“原生”?
服务器·网络·tcp/ip
不灭的程序员阿澄15 小时前
在飞牛 NAS 上用 Docker 运行 DeepSeek Harness
运维·docker·容器
Elastic 中国社区官方博客16 小时前
让大模型思考,让小模型执行:在 Elastic Workflows 中拆分 LLM 成本
大数据·运维·数据库·人工智能·elasticsearch·ai
爱学习的小白柏16 小时前
【AI问数技术】多Agent协同架构:查询规划/SQL生成/洞察分析/报告生成
java·网络·人工智能·windows·sql·架构·llama
艾伦_耶格宇16 小时前
【AI】-4 OpenCode Go 接入 Obsidian 完整指南
运维·开发语言·人工智能·agent·opencode