---
url: https://eoht.net/esxi/openwrt/openwrt-real-time-packet-capture.md
description: >-
  在 OpenWrt 上用 tcpdump 抓包，通过 SSH 将 pcap 数据送入 Wireshark；区分 Windows cmd、PowerShell
  7.4 及以上和 Linux/macOS 的调用方式，并检查网卡、SSH 认证与抓包结果。
---

# OpenWrt 实时抓包：tcpdump、Wireshark 与 Windows 管道 {#openwrt-realtime-packet-capture}

这套方法在路由器运行 `tcpdump`，通过 SSH 把 pcap 二进制数据送给电脑上的 Wireshark。适合检查自己管理的网络中的连接、DNS 和协议问题；HTTPS 等加密流量不会因为抓包就自动变成明文。

## 软件准备 {#software-requirements}

### 1. 安装 tcpdump 并确认接口 {#install-tcpdump-on-openwrt}

先确认固件的软件包管理器。使用 `opkg` 的版本执行：

```sh
opkg update
opkg install tcpdump
```

使用 `apk` 的版本执行：

```sh
apk update
apk add tcpdump
```

安装后检查可抓包接口，不要直接假定所有设备都有 `br-lan`：

```sh
tcpdump --version
tcpdump -D
ip link show
```

下文以 `br-lan`、SSH 端口 `22` 为例；实际接口和端口不同，需要同时修改抓包命令及过滤条件。先在路由器上抓 10 个包验证权限和接口，命令会在捕获足够数据后退出；没有流量时可按 `Ctrl+C` 停止：

```sh
tcpdump -ni br-lan -c 10 'not port 22'
```

### 2. 安装 Wireshark 和终端工具 {#install-wireshark-and-terminal}

<Links
:grid="2"
:items="[
 {
   icon: { icon: 'simple-icons:wireshark', color: '#1679A7' },
   name: 'Wireshark',
   desc: '网络协议分析工具，可以实时捕获和分析网络数据包',
   link: 'https://www.wireshark.org/download.html'
 },
 {
   image: 'https://i.eoht.net/logo/tabby.svg',
   name: 'Tabby',
   desc: '跨平台的终端模拟器，支持 SSH、Telnet 等协议，适用于连接 Openwrt 进行抓包操作',
   link: 'https://tabby.sh/'
 }
]"
/>

Windows 示例另外需要 [PuTTY 官方提供的 Plink](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html)。`plink.exe` 需位于当前命令搜索路径中；Wireshark 的安装路径也要按本机调整。

在 PuTTY 中保存一个名为 `openwrt-capture` 的 SSH 会话，设置路由器地址、SSH 端口和用户名，并配置公钥认证；加密私钥可通过 Pageant 解锁。先正常连接，核对主机密钥指纹，再保存信任记录。随后在 cmd 中确认无需输入密码也能执行远程命令：

```bat
plink.exe -batch -load openwrt-capture "true"
```

没有输出且退出码为 0 才继续。`-batch` 遇到未知主机密钥或认证失败会退出，不会等待密码；不要把密码写进抓包命令。具体设置见 [Plink 官方手册](https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter7.html)。

## 实时抓包 {#capture-packets-in-realtime}

### 按终端选择命令 {#capture-with-terminal}

**Windows cmd：**

```bat
plink.exe -batch -T -load openwrt-capture "tcpdump -U -ni br-lan -s 0 -w - not port 22" | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -
```

**PowerShell 7.4 及以上：**

```powershell
$PSVersionTable.PSVersion
& plink.exe -batch -T -load openwrt-capture "tcpdump -U -ni br-lan -s 0 -w - not port 22" | & 'C:\Program Files\Wireshark\Wireshark.exe' -k -i -
```

Microsoft 说明，PowerShell 7.4 起原生命令之间的管道能够保留字节流。这里两端都必须是原生程序，中间不要插入 `Out-String`、`Tee-Object` 等文本处理。旧版 Windows PowerShell 5.1 或 PowerShell 7.3 及以下请使用 cmd，不能套用上述结论。[版本依据](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7.5#using-native-commands-in-the-pipeline)

**Linux/macOS：** 先确认 `ssh` 和 `wireshark` 在 PATH 中，且已完成 SSH 主机密钥核对和认证，再执行：

```sh
ssh -T root@192.168.1.1 'tcpdump -U -ni br-lan -s 0 -w - not port 22' | wireshark -k -i -
```

macOS 的应用安装位置不一定提供 `wireshark` 命令，可将其替换为实际 Wireshark 可执行文件的完整路径。

| 参数 | 作用 |
| --- | --- |
| `-T` | SSH 不分配伪终端，避免终端处理影响二进制数据 |
| tcpdump `-U` | 以包为单位刷新输出，降低管道显示延迟 |
| `-n -i br-lan` | 不反查地址名称，监听指定接口 |
| `-s 0 -w -` | 使用完整抓取长度，将 pcap 数据写到标准输出 |
| `not port 22` | 排除本例的 SSH 传输，避免抓包连接自身形成大量额外流量 |
| Wireshark `-k -i -` | 启动捕获，并从标准输入读取数据 |

Wireshark 对标准输入的支持见 [命令行手册](https://www.wireshark.org/docs/man-pages/wireshark.html)。不要添加 `2>&1`，否则 SSH/tcpdump 错误文字可能混入 pcap 数据。

## 验证与排查 {#verify-and-troubleshoot-capture}

1. 从局域网设备发起一个已知请求，在 Wireshark 确认时间、源/目标地址和协议符合预期；看见窗口打开不等于抓包成功。
2. 没有数据时，先检查接口、过滤表达式和设备是否确实经过该接口；硬件或软件流量卸载也可能影响能观察到的路径。
3. 提示 pcap 格式错误时，检查 PowerShell 版本、远程登录脚本是否向标准输出打印文字，以及是否分配了伪终端。
4. 结束时停止 Wireshark 捕获并检查终端进程已退出；保存的 pcap 可能含网络标识和明文协议内容，分享前先核对。
