Piping output over SSH

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Piping output over SSH

Postby darkspork » Thu Aug 19, 2010 2:12 pm UTC

Here's a neat little BASH snippet I came up with yesterday to pipe the output of a program to a different terminal over SSH. I'm using it now so I can have a program run on a remote machine (that I cannot SSH into) and track its progress from my desk:

Code: Select all

(echo -e "cat | tee /dev/pts/TERMINAL; logout\n" ; COMMAND) | ssh SSHOPTIONS

I've got a few problems with it, though. First, the output is buffered in several places, so I won't get to see any output for a good five minutes. Second, I want to have the local terminal continue printing output even if the connection fails. I've also tried

Code: Select all

(echo -e "cat > /dev/pts/TERMINAL; logout\n" ; COMMAND | tee /dev/pts/TERMINAL) | ssh SSHOPTIONS

to make up for it, but the output is still buffered. Anybody have any other ideas? Does anyone have any completely different (better, easier, etc...) way to approach this?
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

Pepve
Posts: 57
Joined: Wed Jul 28, 2010 9:47 am UTC

Re: Piping output over SSH

Postby Pepve » Thu Aug 19, 2010 4:37 pm UTC

That's some funky shell scripting! It took me a moment to figure out what it does. Here is what my approach would be, on the receiving end:

Code: Select all

nc -l -p 12345

On the sending end:

Code: Select all

COMMAND | nc -q 0 HOST 12345

I would throw tee, a named pipe (man mkfifo) and tail -f in there to also see the output at the sender.
Btw, the cat in your first snippet seems to be redundant.

Edit: Not funky in a bad way, but funky in a good way. Thought i'd clarify before my untested vocabulary kicks me in the nuts.

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: Piping output over SSH

Postby darkspork » Thu Aug 19, 2010 6:12 pm UTC

Pepve wrote:

Code: Select all

nc -l -p 12345

Code: Select all

COMMAND | nc -q 0 HOST 12345

Knew there was an easier way. Is there a way to encrypt that traffic, though?
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

Pepve
Posts: 57
Joined: Wed Jul 28, 2010 9:47 am UTC

Re: Piping output over SSH

Postby Pepve » Thu Aug 19, 2010 7:30 pm UTC

Apparently there is, i just discovered the incredibly versatile openssl command line tool. Receiver and sender:

Code: Select all

nc -l -p 12345 | openssl enc -d -rc4 -pass pass:KEYPHRASE
COMMAND | openssl enc -rc4 -salt -pass pass:KEYPHRASE | nc -q 0 HOST 12345

But there's an easier way, painfully obvious once you think of it, receiver and sender:

Code: Select all

tail -f fifo
COMMAND | ssh HOST 'cat > fifo'

Of course it doesn't have to be a named pipe, it can be a file, terminal, /dev/null...

Fyi, cryptcat advertises as a crypt-capable netcat, and it works, but it doesn't support the nifty -q option.

Edit: Also, you might want to do COMMAND 2>&1 (redirect stderr to stdout, so you don't miss errors at the receiving end).

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Piping output over SSH

Postby thoughtfully » Fri Aug 20, 2010 2:04 pm UTC

I had a feeling this was a netcat problem, but I didn't dig into it. You can use port farwarding with your ssh connection, and netcat over that port, and it'll be encryped.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

Ayren
Posts: 18
Joined: Thu Feb 18, 2010 1:25 pm UTC

Re: Piping output over SSH

Postby Ayren » Wed Sep 01, 2010 7:39 pm UTC

For an encryption-capable netcat, also check out SOCAT. It can connect pretty much anything to anything (files, processes, sockets, fifo's, etc).

User avatar
tetsujin
Posts: 426
Joined: Thu Nov 15, 2007 8:34 pm UTC
Location: Massachusetts
Contact:

Re: Piping output over SSH

Postby tetsujin » Fri Sep 03, 2010 6:13 pm UTC

darkspork wrote:Here's a neat little BASH snippet I came up with yesterday to pipe the output of a program to a different terminal over SSH. I'm using it now so I can have a program run on a remote machine (that I cannot SSH into) and track its progress from my desk:

Code: Select all

(echo -e "cat | tee /dev/pts/TERMINAL; logout\n" ; COMMAND) | ssh SSHOPTIONS


How about this?

Code: Select all

localcommand | ssh user@hostname 'bash -c "cat | tee whatever"'


ssh reads the password straight from the TTY (not from stdin) so you can still enter your password - but this works better if you don't need to, of course.

(EDIT): Whoops, I guess somebody already mentioned this...
---GEC
I want to create a truly new command-line shell for Unix.
Anybody want to place bets on whether I ever get any code written?


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 17 guests