Run a remote script on a local file via SSH

3

1

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.

Vladislavs Dovgalecs

Posted 2018-02-05T17:23:26.997

Reputation: 133

2Does 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 – 2018-02-05T17:50:40.687

@KamilMaciorowski Yes, the tool can read from stdin. I updated the question. Thanks for the clarifying questions. – Vladislavs Dovgalecs – 2018-02-05T20:34:42.457

Answers

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'

Kamil Maciorowski

Posted 2018-02-05T17:23:26.997

Reputation: 38 429