3

I have a remote machine with a Linux binary that accepts a number of arguments and a text file. The output of a program is written to stdout that I always write to a text file. I found myself copying a data file to the remote machine, executing the command on that file and copying the resulting file back. This is time consuming and error-prone.

Is it possible with SSH and the standard Linux tools to run a remote binary via SSH on a local file without copying it first to the remote machine?

Ideally, I would like to have a bash script on my local machine. I specify the data file as an argument and it performs all the SSH connection, data sending etc and outputs the result on stdout locally.

My local machine is Mac OS X and the remote is Linux. No, I can't get Linux binary to work on Mac OS X.

Update: The tool on the remote machine can read from stdin.

  • 2
    Does the tool need this text file to be seekable? Can the tool accept stdin instead of a text file? If not, does it use stdin besides a text file? – Kamil Maciorowski Feb 5 '18 at 17:50
  • @KamilMaciorowski Yes, the tool can read from stdin. I updated the question. Thanks for the clarifying questions. – Vladislavs Dovgalecs Feb 5 '18 at 20:34
1

If the tool can read stdin instead of a text file and provides its output via stdout, this bash syntax should run it remotely without the need of copying any files:

< "/local_path/to/input_file" ssh user@remote 'the_tool -some_option1 -option2'

Note: some commands need special option or argument (e.g. -) to read stdin; some read stdin while there's no input file specified. I know nothing about your tool, so this is a generic approach that you may need to adjust.

Redirection of the output to a local file with > or (local) tee will work as well:

< "/local_path/to/input_file" ssh user@remote 'the_tool -some_option1 -option2' | tee "/local_path/to/output_file"

Example: remote cat concatenates local and remote /etc/hosts:

< "/etc/hosts" ssh user@remote 'cat - /etc/hosts'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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