Productive Linux


 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Don't have an account?
Sign up to
Forgot your password?

Copying with scp from STDIN
30 October 2010 @ 15:44 BST
by Paul

The premier tool for copying a file or two from one machine to another is scp. It's simple. It's effective. That's why there's a FAQ, which is will scp accept data from STDIN?

The answer is 'no'.

Some people complain about this, but it's not a bug, it's a feature.

If you want to take a stream of data on a computer and dump it in a file, you don't use the cp command, do you? Rather, you do something like this:

echo "some text" > afile.txt

What's true of an operation on one machine is true of an operation across machines. So, if you have a stream of data produced by a script on one machine and you want it in a file on another machine, that's not copying the data, it's storing some data produced on one machine in a file on another machine.

When the problem is reframed like that, a number of solutions become apparent. Probably the easiest is this:

echo "some text" | ssh "cat > /remote/file"

Obviously, echo "some text" should be changed to any command or script that produces a stream of data to STDOUT and the host and file names should be changed as appropriate.

Tags: bash ssh scp

Actually, scp *does* support reading from stdin; it just seems to be undocumented. The scp process on the local machine establishes an ssh connection to the remote machine and then invokes an scp process on the remote machine, and that remote scp process has to read from stdin.

The command > scp foo remote:/path/to/bar

invokes the following on the machine "remote": > ssh -t /path/to/bar

I discovered this while debugging a "validate-rsync" script to use in the "command=" rule for an ssh key.

Posted by John Abreau on 2011-12-22 21:09:20.
Comments disabled