I'm trying to get OpenBSD netcat to listen to UDP connections on a particular port (3333) and interface (10.42.0.1) and send a fixed response. To achieve this I do:
echo "asd" | nc -k -u -l -p 3333 -s 10.42.0.1 -v
And then I send a message by doing:
echo qwe | nc -u 10.42.0.1 3333 -v
The outputs are
# Server side
Bound on h9ct3d3 3333
XXXXXqwe
# Client side
Connection to 10.42.0.1 3333 port [udp/*] succeeded!
As you can see, the client does not receive asd
. However, if I remove the -k
, it does receive it:
##### Server side
echo "asd" | nc -u -l -p 3333 -s 10.42.0.1 -v
Bound on h9ct3d3 3333
# ...
# Wait for the client to execute
# ...
Connection received on h9ct3d3 51318
XXXXXqwe
##### Client side
echo qwe | nc -u 10.42.0.1 3333 -v
# ...
# Wait for the above 'X'to finish
# ...
Connection to 10.42.0.1 3333 port [udp/*] succeeded!
asd
I have tried to use a while loop without the -k
, but nc
never returns:
#!/bin/bash
while true
do
echo "Host message" | nc -q 0 -u -l -p 3333 -s 10.42.0.1 -v
echo "restarting..."
done
How can I make this work?
My version of netcat is:
OpenBSD netcat (Debian patchlevel 1.206-1ubuntu1)
PD. Why does the nc
server 'send' those weird X
characters? The client never reads them, it just waits for them to finish. They seem to be displayed every 1 sec.