in reply to perl -e in ssh

That's because you have an additional shell in the remote side interpreting escapes and quoting.

You need to add an extra level of quoting resulting in...

'perl\\ -e\\ \\\'\\$s\\ \\=\\ shift\\;\\ \\$SIG\\{ALRM\\}\\ \\=\\ sub\ +\ \\{\\ kill\\ INT\\ \\=\\>\\ \\$p\\ \\}\\;\\ exec\\(\\@ARGV\\)\\ unl +ess\\ \\$p\\ \\=\\ fork\\;\\ alarm\\ \\$s\\;\\ waitpid\\ \\+\\$p\\,\\ + 0\\ \\\'\\ 3\\ \\\'ls\\ -ltr\\\''
Or you can use Net::OpenSSH and forget about this quoting nightmare:
use Net::OpenSSH; my $ssh=Net::OpenSSH->new($server); my $line = $ssh->capture(perl => -e => <<'EOC', 3, 'ls -ltr'); $s = shift; $SIG{ALRM} = sub {kill INT => $p}; exec(@ARGV) unless $p = fork; alarm $s; waitpid $p, 0; EOC
And check also GRID::Machine, that allows to run perl code in remote machines through SSH.

Replies are listed 'Best First'.
Re^2: perl -e in ssh
by ikegami (Patriarch) on Apr 13, 2010 at 17:59 UTC
    Or automate the quoting:
    sub shell_quote { my $s = @_ ? $_[0] : $_; return $s if $s =~ /^[a-zA-Z0-9-_]+\z/; $s =~ s/'/'\\''/g; return "'$s'"; } my $server = 'localhost'; my $cmd = 'ls -ltra'; my $timeout = 3; my $watchdog = <<'__EOI__'; $t = shift; $SIG{ALRM} = sub { kill INT => $p }; exec(@ARGV) unless $p = fork; alarm $t; waitpid $p, 0 #exit(...) __EOI__ my $watchdog_cmd = join ' ', map shell_quote, perl => "-e$watchdog", $timeout, $cmd; my $ssh_cmd = join ' ', map shell_quote, ssh => $server, $watchdog_cmd; my $output = `$ssh_cmd`;

    But wouldn't it make more sense to include ssh in the timeout?

    ... my $ssh_cmd = join ' ', map shell_quote, ssh => $server, $cmd; my $watchdog_cmd = join ' ', map shell_quote, perl => "-e$watchdog", $timeout, $ssh_cmd; my $output = `$watchdog_cmd`;

    Tested.

      the above code just prints out ls -ltra instead of the output of the cmd
      ikegami the code just blurts out ksh: 4: not found Please guys can point out where I am going wrong.
      ikegami the code just blurts out ksh: 4: not found also there is something about this line thats not right $s = s/'/'\\''/g ; Please guys can point out where I am going wrong.
        They're both the same problem.
        $s = s/'/'\\''/g;
        should be
        $s =~ s/'/'\\''/g;

        Fixed in original code. Tested.

Re^2: perl -e in ssh
by Anonymous Monk on Apr 13, 2010 at 17:46 UTC
    I dont have OpenSSH module. But I am still getting this error sh: syntax error at line 1: `(' unexpected where else can I correct