17

What is the simplest and most versatile way to send files over the network to other computers? By that I mean computers that other people are using at the moment. I don't think SSH works if the computer has an active session open.

So far I am using netcat, which works alright. But are there any other simple ways to do this? One problem I have with netcat, is that the receiver needs to know the file ending and has to come up with a name for the stream.

TomTom
  • 2,453
  • 6
  • 18
  • 23

5 Answers5

24

You're complicating your life needlessly. Use scp.

To transfer a file myfile from your local directory to directory /foo/bar on machine otherhost as user user, here's the syntax: scp myfile user@otherhost:/foo/bar.

EDIT: It is worth noting that transfer via scp/SSH is encrypted while transfer via netcat or HTTP isn't. So if you are transferring sensitive files, always use the former.

dr_
  • 28,763
  • 21
  • 89
  • 133
  • Don't I need a password for `scp` of the user account? – TomTom Apr 21 '15 at 08:57
  • Yes, of course. You need a password for the account `user@otherhost`. – dr_ Apr 21 '15 at 08:58
  • 2
    No, you don't need a password if you use public key authentication – YoMismo Apr 21 '15 at 13:22
  • 9
    Ok, if you want to be picky, let's state that _you have to authenticate_ as `user@otherhost`. – dr_ Apr 21 '15 at 13:31
  • 1
    Also, as a simplification, if your username is the same on both machines, you can exclude the username. It'll still ask you for a password when applicable, though. – Sildoreth Apr 21 '15 at 20:17
  • Setup ssh-agent and it will cache the authentication creds for duration of your session. In other words, you will only have to type the password once per session/login, not each time you use scp. – Walter Apr 22 '15 at 00:35
  • scp can put encryption performance bottleneck when using awfully slow machines – andrej Nov 22 '16 at 15:20
  • 1
    Such a no brainer, yet didn't cross my mind at all! Thanks! – aksh1618 Nov 05 '21 at 05:58
14

If you're happy with netcat you can work around the file name issue by intruducing tar. This also simplifies sending multiple files at once as well as sending directories.

On the sending side use:

tar cf - <files> | nc <host> <port>

And on the receiving side:

nc -l <port> | tar x

Another solution would be to use rsync or scp.

Marco
  • 33,188
  • 10
  • 112
  • 146
  • I usually add -q2 to the sending `netcat` as well, to make it shut down the connection at the end of the stream. – Simon Richter Apr 21 '15 at 16:59
  • I fail to understand why this should be required. It does shut down automatically without the `-q` option. – Marco Apr 21 '15 at 17:40
  • That depends on which netcat implementation you use. There are implementations that only close one direction of the TCP stream when stdin closes, because the other direction may still be active. – Simon Richter Apr 21 '15 at 18:40
  • `tar | ssh tar` also works very nicely in cases where scp falls down and rsync isn't available. – hobbs Apr 22 '15 at 07:15
12

You can also try

python -m SimpleHTTPServer 8180

It will serve the files in directory in which it executed over HTTP, you can access it via Browser.

Anshul Patel
  • 641
  • 5
  • 11
2

Of course ssh works if another session is open. You can just do

ssh user@host cat /path/to/file.tar  > localfile.tar

Or, to copy to your local directory:

scp user@host:/path/to/file.tar .
terdon
  • 234,489
  • 66
  • 447
  • 667
  • Is it possible the other way around e.g. `ssh lubuntu '>newfile.txt' – heemayl Apr 21 '15 at 10:20
  • 1
    @heemayl yes, to cat into a remote file, you would do `cat local.file | ssh user@host "cat > remote.file"`, or `ssh user@host "cat > remote.file" < local.file`. – terdon Apr 21 '15 at 10:34
0

If both hosts are in the same LAN, you can use woos.

It's extremly simple in usage.

If sender and receiver are ANYWHERE in the internet and you have to transfer BIG FILES, you should install F*EX: http://fex.belwue.de/index.html

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
Framstag
  • 169
  • 1
  • 4
  • 1
    basic scp is also extremely simple, and a tool which is actually distributed, trusted and used by millions people, unlike these home-made tools which appear to be promoted by their author. – Jack Wasey Jul 19 '18 at 13:47