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 user@remotehost.com "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 user@remotehost.com "cat >/data/backup.tar.lzma.gpg"
Thanks!
Nice site btw
Pingback: stdin/stdout
add pipe view (pv) to the before ssh, you’ll get a ncie progress indicator
@0dmin That’s a great idea!
q.v. pv manpage
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.
@Maik K pv has the -L option to limit the transfer.
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.
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.
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”