Given your update, where you have a piece of code on machine A, and you want to execute it on machine B, my solution is to put the code in a standalone file, copy it to the remote machine, and then execute it as normal over ssh. In my case, I have a shared filesystem (machines B through Z all mount a filesystem off machine A), so this is a trivial File::Copy. However, my code actually also handles test scenarios where the shared filesystem isn't yet mounted, and starts by doing an scp first. Because I'm not sharing ssh connections, this does necessitate a second ssh connection, but in my case, that's pretty trivial, and since I now actually use sshfs to mount that shared filesystem when it isn't already mounted via nfs, I don't even need that much.

Trying to copy arbitrary amounts of code across and execute the code all in a single go is going to be far from trivial. However, there's no reason that my previous solution wouldn't work if you formulate your code as a one-liner, e.g., if you set:

@cmd = ( '/usr/bin/perl', -e => quote_cmd($script) );
(don't put leading/trailing quotes on the script - the $script value should just be the code), this should be fine. However, this doesn't satisfy E(). You simply can't do it that way. You'll need to do:
@cmd = ('perl', '-e', $script); # pick which one you need: system(@cmd); system('ssh', $host, quote_cmd(@cmd)); system('ssh', $proxy, quote_cmd('ssh', $host, quote_cmd(@cmd))); # etc., etc.,
Basically, every time you add a new ssh layer, you'll have to re-quote_cmd it.


In reply to Re: safely passing args through ssh by Tanktalus
in thread 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.