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
#!/usr/bin/perl
use strict;
use warnings;
my $cmd = 'ping';
my $host = '127.0.0.1';
my $timeout = 3;
my @cmd = ();
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";
}
1 2 3 4 5 6 7 8
while ( my $line = <$pipe> ) { push @cmd, $line; if ( $line =~ m/password prompt:/ ) { # define your pattern for the password prompt # maybe empty or reset @cmd last; # end while loop } } close $pipe;
1 2 3 4 5 6 7 8 9 10 11
while ( my $line = <$pipe> ) { if ( $line !~ m/password prompt:/ ) { # define your pattern for the password prompt push @cmd, $line; } else { # maybe empty or reset @cmd last; # end while loop } } close $pipe;
2022-06-14T12:06:11 peterbAn diesem Punkt frägt es nach einem Passwort und hier will ich diesen Vorgang abbrechen und im Script weiter machen.
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 37
#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; sub readInput { ReadMode(4); my $line = ""; my $key = " "; my $other_condition = 100; # 10 is the ord() of the newline character: while (ord($key) != 10) { # You can check for other conditions during input here: if ($other_condition != 100) { print "\n"; last; } while (not defined ($key = ReadKey(-1))) { } print $key; if (ord($key) != 10) { $line .= $key; } # $other_condition = 20; } ReadMode(0); return $line; } my $result = readInput(); print "Result is: $result\n";