Reputation: 2061
I need to give the user ability to send/receive messages over the network (using netcat
) while the connection is stablished (the user, in this case, is using nc
as client). The problem is that I need to send a line before user starts interacting. My first attempt was:
echo 'my first line' | nc server port
The problem with this approach is that nc
closes the connection when echo
finishes its execution, so the user can't send commands via stdin because the shell is given back to him (and also the answer from server is not received because it delays some seconds to start answering and, as nc
closes the connection, the answer is never received by the user).
I also tried grouping commands:
{ echo 'my first line'; cat -; } | nc server port
It works almost the way I need, but if server closes the connection, it will wait until I press <ENTER>
to give me the shell again. I need to get the shell back when the server closes the connection (in this case, the client - my nc
command - will never closes the connection, except if I press Ctrl+C).
I also tried named pipes, without success.
Do you have any tip on how to do it?
Note: I'm using openbsd-netcat.
Upvotes: 3
Views: 1066
Reputation: 1433
It is cat
that wait for the 'enter'.
You may write a script execute after nc
to kill the cat
and it will return to shell automatically.
Upvotes: 1
Reputation: 1
I would suggest you use cat << EOF
, but I think it will not work as you expect.
I don't know how you can send EOF
when the connection is closed.
Upvotes: 0
Reputation: 21
This one should produce the behaviour you want:
echo "Here is your MOTD." | nc server port ; nc server port
Upvotes: 0
Reputation: 7640
You can try this to see if it works for you.
perl -e "\$|=1;print \"my first line\\n\" ; while (<STDIN>) {print;}" | nc server port
Upvotes: 0