How do I run a command for each UDP packet with socat?

3

I'm trying to print each arriving UDP packet. I'd like to use xxd to print the ascii contents as well as the raw data (as hex).

So far I've used socat - udp4-listen:11255,reuseaddr,fork | xxd

This sort of works, but xxd buffers the input until it can print a complete line of output.

I'd like socat to execute a new command for each arriving udp packet. I've tried

socat -u udp4-listen:11255,reuseaddr,fork system:"/usr/bin/xxd - /tmp/foo"

It forks a new xxd for each packet (which is good) but keeps it running instead of closing the input after passing the packet.

Is there a way to make socat close the input of the forked process after passing the UDP packet?

Jiri Kraml

Posted 2019-04-05T09:57:45.010

Reputation: 61

Answers

3

socat -u udp4-recvfrom:11255,reuseaddr,fork exec:"/usr/bin/xxd - /tmp/foo"

does it. The difference is using udp4-recvfrom instead of udp4-listen. The former seems to handle input per packet, while the latter concatenates packets into a stream.

Jiri Kraml

Posted 2019-04-05T09:57:45.010

Reputation: 61