praiseHellRaiseDale
praiseHellRaiseDale

Reputation: 2270

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

I'm trying to create some test network traffic by sending TCP traffic using netcat.

The issue is that the netcat command to receive TCP data doesn't end on its own, which makes perfect sense.

I'm wondering how I can have a script that continues running to the send portion of the TCP traffic. Something similar to:

#!/bin/bash

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

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

Obviously the script will never get to the #Send traffic line because the receive traffic never ends. I've been looking at methods for running functions in bash asynchronously, but nothing I've read seems to solve this problem.

Upvotes: 2

Views: 2336

Answers (1)

Inian
Inian

Reputation: 85530

How about just running receive traffic process in the background so that the other commands can be executed to?

# 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

Upvotes: 2

Related Questions