5

When testing certain multicast connectivities in the past I have been using socat to do this:

socat -u UDP4-RECVFROM:12345,add-membership=224.1.2.3:eth0,fork,reuseaddr,readbytes=32 SYSTEM:"hexdump -C"

and then ctrl-c when I see some packets or after a second or two I see nothing.

Now this obviously gets impractical when having to do this for hundreds of multicast addresses, especially when when some of them send tens of thousands of packets per second (load is at 5000 quicker than I could hit ctrl-c).

How can I limit socat to only receive one packet and then exit? Also, how can I set a timeout for when nothing has been received (I tried -T parameter, but it doesn't seem to have any effect)

1 Answer 1

1

The socat cmdline switch -T seems to only work with UDP4-RECV: and not UDP4-RECVFROM:. But with UDP4-RECV the ,fork is not working, which you need to stop after the first packet.

This leaves you with the following possiblitiy:

socat -u udp-recvfrom:8500,add-membership=224.1.2.3:eno1,readbytes=32,fork SYSTEM:"hexdump -C && kill 0" & pid=$!; sleep 10;kill $pid

Where the ,fork will start a new process for each udp packet, the SYSTEM:"hexdump -C && kill 0" will kill the socat processgroup after that one packet. The readbytes=32 I left there because you had it in your original cmdline, so it will read at most 32 bytes of a packet.

The Timeout if no packet is arriving part is a bit tricky and i have no nice solution, since we need to background, the socat, sleep and then kill the socat.

If a packet got received, the kill $pid will not find that process anymore obviously and it will get an error. This can be worked around with checking if it is still alive.

Anyway, this should work.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.