Sending file via netcat

25

11

I'm using something like this to send file from one computer to another:

To serve file (on computer A):

cat something.zip | nc -l -p 1234

To receive file (on computer B):

netcat server.ip.here. 1234 > something.zip

My question is... can I do the opposite? Let's say I have file on computer B and I want to send it to A but not the way I wrote above, but by making computer that's supposed to receive file (A) be 'listening' server and connect computer that's 'sending' file (B) to server and send the file? Is it possible? I think it might be but I'm not sure how to do this.

In case my above explanation is messed up: How do I send file TO 'server' instead of serving the file on server and then taking it FROM it (like I did above)?

Phil

Posted 2010-01-20T03:39:13.650

Reputation: 1 031

Note: if you're using nc because scp is too slow and you don't need encryption, you may want to switch to udpcast: https://superuser.com/questions/692294/why-is-udcast-is-many-times-faster-than-netcat

– unhammer – 2018-06-19T12:07:37.117

Answers

40

On your server (A):

nc -l -p 1234 -q 1 > something.zip < /dev/null
On your "sender client" (B):
cat something.zip | netcat server.ip.here 1234

martinwguy

Posted 2010-01-20T03:39:13.650

Reputation: 642

Yeah, I thought that I'll have to switch sides somehow but I wasn't sure how :). Thanks, it works! – Phil – 2010-01-21T04:23:28.607

That's a cute netcat trick. – DaveParillo – 2010-01-21T16:09:41.683

With -q 1, I received only a 20-50% of a 1.2GB file. It worked fine with a higher -q value. – Ivan Kozik – 2017-01-13T07:34:57.710

-q seconds :: after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever. For me, a -q value of -1 worked for a multi-gig file. – ThorSummoner – 2017-03-21T15:52:56.577

4if you have pv, I would suggest operating the "server client" as pv soemthing.zip | ..., that will display the progress of the file read going into the pipe. – ThorSummoner – 2017-03-21T15:54:00.437

With me and -q 0 it works OK for large files. Maybe it's a bug that was fixed? – rogerdpack – 2018-09-28T04:13:35.973

2why the < /dev/null part? – törzsmókus – 2012-11-27T11:17:52.750

7Because netcat both reads stdin and writes stdout simultaneously, sending anything read from stdin out to the network and printing anything received from the network on stdout. The </dev/null ensures that, when the received file has completed, netcat doesn't sit there indefinitely waiting for input from the console (which it shouldn't be reading anyway!) and also ensures that it doesn't gobble up any input that may be available from the terminal, such as you typing the next command you wanted to run. – martinwguy – 2013-01-04T07:18:03.013

6

As a note, if you want to also preserve file permissions, ownership and timestamps, we use tar with netcat to do transfers of directories and files.

On receiving system:

nc -l -p 12345 -q 1 | tar xz -C /path/to/root/of/tree

From sending system:

tar czf - ./directory_tree_to_xfer | nc <host name or IP address of receiving system> 12345 

Hope that helps.

B.Kaatz

Posted 2010-01-20T03:39:13.650

Reputation: 171

1Good addition to the answer, but please leave it as a comment and not as a separate answer. – agtoever – 2015-02-27T00:33:29.537

2Couldn't add a comment above due to lack of Rep points on this sub-board, so I had to offer it as an answer, though apparently, you are allowed to comment on your own answers. – B.Kaatz – 2015-03-12T23:30:16.663

3

Computer A: nc -l -p 1234 > filename.txt

Computer B: nc server.com 1234 < filename.txt

Should work too ;)

mark

Posted 2010-01-20T03:39:13.650

Reputation: 31

0

Start another instance of netcat on computer B. Just do what you did on computer A, but serve it from B. Give the new server a new port.

DaveParillo

Posted 2010-01-20T03:39:13.650

Reputation: 13 402

1As you can see from accepted answer - yes, that's how netcat can work too. – Phil – 2010-01-21T04:24:26.780

You're right. I learn something new every day! – DaveParillo – 2010-01-21T16:10:14.847

I don't want to do the same, I want to change commands to something else (i'm not sure if it's possible)... look at bold text in my question. What I mean is that I don't want to serve from B. I want A to be server listening for input... and then I want to send file contents from non-listening B to listening A. – Phil – 2010-01-20T05:51:40.087

That's not how netcat works. – DaveParillo – 2010-01-20T15:14:47.640