如何在 Bash 中使用 netcat 自动发送和接收 TCP 流量?

How can I automate sending and receiving TCP traffic using netcat in Bash?

我正在尝试通过使用 netcat 发送 TCP 流量来创建一些测试网络流量。

问题是用于接收 TCP 数据的 netcat 命令不会自行结束,这很合理。

我想知道如何让脚本继续 运行 TCP 流量的发送部分。类似于:

#!/bin/bash

#Prepare to receive traffic
nc -vv -l -p 5000 > receiveData.txt

#Send traffic
nc -nvv localhost 5000 < sendData.txt

显然脚本永远不会到达 #Send traffic 行,因为接收流量永远不会结束。我一直在异步地查看 bash 中 运行 函数的方法,但我读过的任何内容似乎都无法解决这个问题。

如何只 运行 在后台接收流量进程以便可以执行其他命令?

# Prepare to receive traffic
# Running this process in the background so that the sending TCP traffic
# happens

nc -vv -l -p 5000 > receiveData.txt &

#Send traffic
nc -nvv localhost 5000 < sendData.txt