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.
In reply to Re^2: perl -e in ssh
by ikegami
in thread perl -e in ssh
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |