Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to do this:

ssh user@socket command /path/to/file/on/local/machine

That is to say, I want to execute a remote command using a local file in one step, without first using scp to copy the file.

share|improve this question
It's possible if the remote computer has a network mount of a parent directory of the file of the local machine or if you redirect the file into the ssh command – h3rrmiller Oct 26 '12 at 17:24

2 Answers

up vote 4 down vote accepted

You missed just one symbol =)

ssh user@socket command < /path/to/file/on/local/machine
share|improve this answer
Of course! I wonder why I didn't consider input redirection over ssh?! – trideceth12 Oct 26 '12 at 13:08
What if remote command can only take a file argument and not read from stdin? – Chandra Ravoori Oct 26 '12 at 14:48
@ChandraRavoori in that case you need to copy it with scp before. – rush Oct 26 '12 at 15:53
2  
@ChandraRavoori You can try giving it the file argument /dev/stdin or -. May or may not work (/dev/stdin is a file, but seeking it will fail) – derobert Oct 26 '12 at 16:03
@derobert and rush, played around with it a bit and discovered a hacky way to do it using process substitution under bash. This is still subject to the seeking limitation and can get unwieldy with all the additional quoting that will be needed. Example follows. cat test.file | ssh user@machine 'bash -c "wc -l <(cat -)"' – Chandra Ravoori Oct 26 '12 at 19:04

One way that works regardless of the command is to make the file available on the remote machine via a remote filesystem. Since you have an SSH connection:

  1. Establish a reverse SSH tunnel. See also SSH easily copy file to local system
  2. Mount a directory tree of your machine containing the file to share on the remote machine with SSHFS. (Example)
share|improve this answer
Anyway this way requires additional movements before command execution. – rush Oct 27 '12 at 6:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.