How to send a file using netcat and then keep the

2020-06-17 04:07发布

问题:

I am issuing the command:

netcat serveraddress myport < MY_FILE

The thing is, that netcat sends a message to close the connection once the file is sent. I need to write messages from the console after sending that file. I remember to have done something to pipileline to stdin.. was it this?

netcat serveraddress myport < MY_FILE | 

That isn't working now.

I'm on unix.

Extra info: This did not assume control on server side (E.G. use netcat on serverside to listen for the inbound connection)

回答1:

Perhaps you were doing:

cat MY_FILE - | ncat ...

(Note that I've intentionally mispelled netcat, because I believe ncat is a superior program.)



回答2:

Server side:

nc -k -l 10000 < my_in_file

Client side:

echo "bye" | netcat 192.168.1.6 10000 > my_in_file -


回答3:

To keep listening for other connections use -k on nc.

suppose you want to make connection to server , server write to file and print to stdout ?

Server:

nc -k -l $PORT | tee file ( or > file without print to stdout)

Client

nc $IP $PORT < file_to_send


回答4:

You can use the -q -1 option of nc:

echo MY_FILE | nc -q -1 192.168.0.1 9000

This way it will also work if the command is run in background.



回答5:

I realise this thread is very old but and the OP uses unix, for reference here is a windows equivalent to "cat FILE - | ncat HOST":

type FILE con | ncat HOST

Then type CTRL-Z or CTRL-C to end the connection.

Notes

  • 'con' means console input, see http://ss64.com/nt/type.html
  • It works with binary files.