in reply to Re: perl -e in ssh
in thread perl -e in ssh

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.

Replies are listed 'Best First'.
Re^3: perl -e in ssh
by Anonymous Monk on Apr 13, 2010 at 18:42 UTC
    the above code just prints out ls -ltra instead of the output of the cmd
Re^3: perl -e in ssh
by Anonymous Monk on Apr 13, 2010 at 19:05 UTC
    ikegami the code just blurts out ksh: 4: not found Please guys can point out where I am going wrong.
Re^3: perl -e in ssh
by Anonymous Monk on Apr 13, 2010 at 19:06 UTC
    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.