One problem with ssh is that it invokes a shell to exec your remote command and the arguments are re-interpreted via that shell.

For example, let argcount be the following perl script:

#!/usr/bin/env perl print "arg count: ", scalar(@ARGV), "\n";
Then running argcount 'a b c' returns 1, but running ssh localhost argcount 'a b c' returns 3. Moreover, running ssh localhost argcount 'a;b;c' exhibits even more undesirable behavior.

So, suppose you want to execute a command like perl -e ... that will work as expected even if it executed remotely via ssh. Clearly ... cannot contain any spaces or shell meta-characters. The question is: what's a good way of encoding ... so that it will survive an ssh call?

To be specific about the problem, let backticks() be defined as follows:

sub backticks { open(my $fh, "-|", @_) or die "fork failed: $1"; do { local($/); <$fh> } }
The problem is to define a function E() such that all of these give the same results:
backticks('perl', '-e', $x); backticks('perl', '-e', E($x)); backticks('ssh', $host, 'perl', '-e', E($x)); backticks('ssh', $host1, 'ssh', $host2, 'perl', '-e', E($x));
Here $x is a perl string containing arbitraty perl source code. Note that the last backticks example likely will preclude any approach which relies on using backslashes to escape meta-characters, although I'm not totally sure about this.

Here's an example of a possible solution:

sub E { my $f = 'H'.(2*length($_[0])); return "eval(pack(q/$f/,q/" . unpack("H*",$_[0]) . '/))'; }
The idea is to hex-encode the string to ensure that the result doesn't contain any spaces or shell meta-characters.

Are there any other ways of solving this problem?

Update: Note that I am not looking to encode an arbitrary shell command. Another statement of the problem is this:

Given an array ref $invoke_perl which will invoke a perl interpreter via open(..., "-|", @$invoke_perl), and given a scalar $script containing perl source, how do I encode $script (resulting in E($script)) so that:

open(..., "-|", @$invoke_perl, '-e', E($script))
will pass the arguments ['-e', $script] to the perl interpreter?

In reply to safely passing args through ssh by perl5ever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.