perl5ever has asked for the wisdom of the Perl Monks concerning the following question:
For example, let argcount be the following perl script:
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.#!/usr/bin/env perl print "arg count: ", scalar(@ARGV), "\n";
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:
The problem is to define a function E() such that all of these give the same results:sub backticks { open(my $fh, "-|", @_) or die "fork failed: $1"; do { local($/); <$fh> } }
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.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's an example of a possible solution:
The idea is to hex-encode the string to ensure that the result doesn't contain any spaces or shell meta-characters.sub E { my $f = 'H'.(2*length($_[0])); return "eval(pack(q/$f/,q/" . unpack("H*",$_[0]) . '/))'; }
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:
will pass the arguments ['-e', $script] to the perl interpreter?open(..., "-|", @$invoke_perl, '-e', E($script))
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: safely passing args through ssh
by ikegami (Patriarch) on Jul 25, 2011 at 08:18 UTC | |
by perl5ever (Pilgrim) on Jul 25, 2011 at 15:06 UTC | |
by ikegami (Patriarch) on Jul 25, 2011 at 18:19 UTC | |
by perl5ever (Pilgrim) on Jul 25, 2011 at 23:02 UTC | |
by ikegami (Patriarch) on Jul 25, 2011 at 23:29 UTC | |
| |
by ikegami (Patriarch) on Jul 25, 2011 at 18:20 UTC | |
|
Re: safely passing args through ssh
by JavaFan (Canon) on Jul 25, 2011 at 09:07 UTC | |
|
Re: safely passing args through ssh
by Tanktalus (Canon) on Jul 25, 2011 at 16:05 UTC | |
|
Re: safely passing args through ssh
by salva (Canon) on Jul 25, 2011 at 19:27 UTC | |
|
Re: safely passing args through ssh
by Tanktalus (Canon) on Jul 26, 2011 at 17:05 UTC | |
|
Re: safely passing args through ssh
by afoken (Chancellor) on Jul 27, 2011 at 13:40 UTC | |
by perl5ever (Pilgrim) on Jul 29, 2011 at 21:37 UTC |