1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/perl use strict; use warnings; my $cmd = '/etc/ping'; my $host = '127.0.0.1'; my $timeout = 3; my @cmd = (); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; print "fuehre exec aus: $cmd -c 5 $host\n"; @cmd = `$cmd -c 5 $host`; alarm 0; }; if ($@) { foreach (@cmd){ print; } print "TimedOut - $@\n"; warn unless $@ eq "alarm\n"; } else { foreach (@cmd){ print; } print "Timed Nicht Out\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/perl # https://www.perl-community.de/bat/poard/thread/19553 use strict; use warnings; my $cmd = 'ping'; my $host = '127.0.0.1'; my $timeout = 3; my @cmd = ( "no output yet" ); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; open my $pipe, '-|', "$cmd -c 5 $host" or die "Could not open pipe: $!"; while ( my $line = <$pipe> ) { push @cmd, $line; } close $pipe; alarm 0; }; if ($@) { print "Output: @cmd\n"; print "timed out - $@\n"; warn unless $@ eq "alarm\n"; } else { print "Output: @cmd\n"; print "No time out\n"; }