Email or username:

Password:

Forgot your password?
Darius Kazemi

Today I learned that `ssh` interacts with stdin/stdout normally. So you can pipe data from machine1 into a command on machine2!

For example, my local machine is a Mac that uses `pbcopy` to put stuff on the clipboard. Ex:

$ echo 'foo' | pbcopy

puts "foo" on my clipboard. If I'm ssh'ed in to a remote machine & I'm running sshd locally, I can do

$ echo 'foo' | ssh myLocalMachineIP pbcopy

and that puts the output of the script from the remote machine onto my local clipboard!

11 comments | Expand all CWs
ranjit

@darius whoa, i've often wanted exactly to be able to use pbcopy and pbpaste on my linux vm, and now i can!

Darius Kazemi

Apparently this is something that is often used in place of scp or rsync or similar. The basic idea is: use `tar` to compress files on machine 1, pipe to machine 2, untar on machine2.

This article also mentions piping the output of `dd` to `ssh` to create a disk image on a remote machine which seems fully insane in a good way:

maketecheasier.com/ssh-pipes-l

SlightlyCyberpunk

@darius Yes! Pipes never cease to amaze :) I once attempted to make a script to do system monitoring on remote systems by creating a FIFO named pipe file on the remove system, running commands like vmstat and having them dump to that pipe file (formatted with awk), and cat-ing the whole thing across an SSH link to a local named pipe. Then you could build a tree structure reading and writing those pipe files down to one master node...it worked alright but it was quite insane :D

ed(1) conference

@darius I've used the reverse method to write a disk image to a remote-booted-to-a-rescue-environment

gzip -c disk.img | ssh root@rescue.example.com "gunzip | dd of=/dev/$ROOTDISK/ bs=1M"

(also known as "how I got FreeBSD installed on an OVH VPS")

michcio (backup)

@darius I don't know if clipboard stuff will work on Linux (ssh session might not have the x11 desktop variable set?) but I think I might have piped something into a SQL shell over ssh at some point. Maybe even compressed it on fly with xz in the pipeline

Maybe it's Eyesaline

@darius Yep! I use this fairly frequently to stream large directories:

tar czf foo - | ssh otherhost tar -C /dest/dir -xzvf -

The verbose flag on the receiving end means it prints the filename when it's on the remote disk, rather than read off the local one.

k1

@darius ssh remote echo 'foobar' | pbcopy

𝗔rtilect𝗭ed

@darius This is something I wish I had known about a few hundred times over the last decade or so. Thanks.

Elias Mårtenson

@darius The way I usually copy an entire directory from one machine to another is using an ssh pipe:

ssh source-machine 'cd /foo && tar cf - source' | tar xf -
Darius Kazemi

@loke yeah I mention that in the follow-up

Go Up