scp from stdin

      9 Comments on scp from stdin

Unfortunately, there is no way to pipe data directly to scp simply because scp can’t read from stdin. But you can abuse ssh to achieve the same result

tar cz a/dir | ssh "cat >outfile.tar.gz"

You can even do some funny things like

tar c /data/ | lzma -c -z |\
gpg --batch --force-mdc -ac -o - --passphrase-fd 3 -c 3< /etc/gpgpassphrase |\
ssh "cat >/data/backup.tar.lzma.gpg"

9 thoughts on “scp from stdin

  1. Pingback: stdin/stdout

  2. avatarMaik K

    I use this a lot, but it has some pretty ugly drawbacks.. When you can’t use scp(like taking disk images via dd and piping them over gzip to the ssh) you have no rate limiting like scp’s “-l” option – that can get pretty nasty if you’re copying stuff between production servers and need to limit it so importand data still can be transferred.

  3. avatarJohn Abreau

    I just discovered that scp actually *does* support reading from stdin, but it doesn’t seem to be documented anywhere.

    “scp -t foo” will listen for the scp protocol on stdin and write the decrypted data to the file or directory “foo”. I discovered this just now while testing a “validate-rsync” script to limit what commands my ssh key could invoke.

    When I invoked it in an xterm, on both Fedora and MacOSX, it tried to read from stdin.

    Apparently the “-t” option is undocumented; it wasn’t in the scp man page on either system, and “scp –help” doesn’t show it. I found this discussion thread while googling for “scp from stdin” to see if I could find any mention of the +-t” option. No luck so far.

  4. avatarRobert Spendl

    Thank you for an excellent hint. I’ve had much trouble with scp on OpenWRT/Dropbear where a script was hanging due to scp awaiting “unknown host” confirmation every time the router has restarted. While ssh has an “-y” option to accept new hosts, scp does not, so your workaround was a life saver.

  5. avatarZeque

    Perfect! thank you very much!
    I needed to clon a hardisk by network. I’ve done this:
    cat /dev/sda | ssh root@remote_ip “cat >/root/sda.img”

Leave a Reply

Your email address will not be published.