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`;