尝试在PERL中分叉多个ping请求时无法打印阵列

时间:2014-10-07 15:59:59

标签: perl

我正在尝试使用PERL中的fork ping 2 IP。我正在推动在不同阵列中可达且不可达的IP。在程序结束时我打印数组,但我没有得到输出

以下是代码

use strict;
use warnings;
my (@up, @down, @child, @test);

sub prog1 {
    my $ip = $_[0];
    print "Started testing IP : $ip \n";
    my $resp = `ping $ip`;
    if ( $resp =~ /reply/) {
        push (@up, $ip);
    }
    else {
        push (@down, $ip);
    }
    push (@test, $ip);
    print "Ended with testing of ip $ip \n";
}

my @list = qw /192.168.1.1 192.168.2.1/;
foreach (@list) {
    my $pid = fork();
    if ( !$pid ) {
        prog1($_);
        exit 0;
    }
    else {
        push (@child, $pid);
    }
}

foreach (@child) {
    waitpid ($_, 0);
    print "Done with pid $_ \n";
}

print "\n\n up is @up";
print "\n\n down is @down\n";
print "test is @test";

我得到的输出是

$ perl fork_ping.pl
Started testing IP : 192.168.1.1
Started testing IP : 192.168.2.1
Ended with testing of ip 192.168.1.1
Done with pid 99804
Ended with testing of ip 192.168.2.1
Done with pid 81456


 up is

 down is
test is

如您所见,@up@down数组为空。任何线索,为什么它没有填充任何IP

0 个答案:

没有答案