29

I want a UDP echo server to get packets, and reply exactly what it has received. How can I simply do this using netcat or socat? It should stay alive forever and handle packets coming from several hosts.

Mohammad Hedayati
  • 629
  • 1
  • 6
  • 12
  • 8
    Please don't make this thing Internet-accessible. Since it's UDP-based it can be used to send a packet stream to arbitrary destinations using source-forged packets. – Evan Anderson Jan 06 '12 at 05:47

6 Answers6

27

Another netcat-like tool is the nmap version, ncat, that has lots of built in goodies to simplify things like this. This would work:

ncat -e /bin/cat -k -u -l 1235

-e means it executes /bin/cat (to echo back what you type)
-k means keep-alive, that it keeps listening after each connection
-u means udp
-l 1235 means that it listens on port 1235

Mattias Ahnberg
  • 4,039
  • 18
  • 19
17

I used socat -v PIPE udp-recvfrom:5553,fork to run the server and socat - udp:localhost:5553 for clients. This was a great help!

Mohammad Hedayati
  • 629
  • 1
  • 6
  • 12
  • 5
    An alternative, coming from http://stackoverflow.com/a/35857095/222529 is `socat TCP4-LISTEN:2000,fork EXEC:cat` – Jir Mar 10 '17 at 17:05
9

You can also use socat (rather than using netcat) as echo server and netcat as client.

Socat echo server (listens on TCP port 1234):

socat -v tcp-l:1234,fork exec:'/bin/cat'

Netcat client (connects to serverip on TCP port 1234):

nc serverip 1234
Andre Miras
  • 201
  • 2
  • 3
2

You can write a C program that forks nc -u -l -p 4321 and then uses dup(2) to connect:

  • nc stdin with the parent's stdout
  • nc stdout with the parent's stdin

Then in an endless loop the parent reads from stdin and writes in stdout whatever the parent reads.

adamo
  • 6,867
  • 3
  • 29
  • 58
  • I can use `socat -v PIPE UDP-LISTEN:5555,fork`, but the second connection gets refused. By the way, it works fine with TCP-LISTEN. I am avoiding to code, otherwise I could write it all in C. – Mohammad Hedayati Jan 04 '12 at 13:16
  • You have to wrap the whole thing in an endless loop: while true; do socat stuff; done – adamo Jan 04 '12 at 18:53
  • You may also get some inspiration using [this tutorial](http://zarb.org/~gc/html/udp-in-ssh-tunneling.html) – adamo Jan 04 '12 at 19:16
2

netcat solution pre-installed in Ubunutu

The netcat pre-installed in Ubuntu 16.04 comes from netcat-openbsd, and has no -c option, but the manual gives a solution:

sudo mknod -m 777 fifo p
cat fifo | netcat -l -k localhost 8000 > fifo

Then client example:

echo abc | netcat localhost 8000

TODO: how to modify the input string value? The following does not return any reply:

cat fifo | tr 'a' 'b' | netcat -l -k localhost 8000 > fifo

The remote shell example however works:

cat fifo | /bin/sh -i 2>&1 | netcat -l -k localhost 8000 > fifo

I don't know how to deal with concurrent requests simply however.

0

If you are running on Windows and use a unix like environment like cygwin, netcat migth not provide the -e parameter. This worked just fine for me.

http://bansky.net/echotool/

Chris
  • 143
  • 4