Iptables – How to redirect (one way only) udp packets to another host using netcat

iptablesnetcat

How do you redirect (one way only) udp packets to another host using netcat?

nc -l -u 0.0.0.0 12345 | nc -u 192.168.1.128 12345

stops after successfully redirecting the first packet.

(Note: an iptables solution would also be useful.)

Thanks,

Chris.

Best Answer

For the iptables solution, you'll basically be doing an destination NAT on the packets. Something like:

iptables -t nat -I PREROUTING -p udp --dport 12345 -j DNAT --to 192.168.1.128:12345

With netcat, hmm. You can use the -k option to keep the listen side up after you process the packets, but you'll need to do something to keep sending. Named pipes, maybe?

mknod /tmp/nc.pipe p
nc -l -k -u 0.0.0.0 12345 > /tmp/nc.pipe &

while [1]
do
  nc -u 192.168.1.128 12345 < /tmp/nc.pipe
done

Untested, clearly.