我在 while 语句中写了这段小代码while(<PS>)
,但它不会执行并读取代码的输出open(PS,"blabla")
。
我该如何解决这个问题?
#!/usr/bin/env perl
#
my $IPendnum = 1;
my $IPrange = 0;
my $START = 1;
while ($START == 1) {
print "Pinging IP : 192.168." . $IPrange . "." . $IPendnum . "\n";
open (PS, "fping 192.168." . $IPrange . "." . $IPendnum);
while(<PS>) {
chop ($_);
if (/is alive/) {
print "The following IP : 192.168." . $IPrange . "." . $IPendnum . " is online! \n";
} else {
print "The following IP : 192.168." . $IPrange . "." . $IPendnum . " is currently offline! \n";
}
}
if ($IPendnum >= 255) {
$IPrange += 1;
$IPendnum = 1;
} else {
$IPendnum += 1;
}
print "Moving to the next IP address \n";
sleep(1);
}
答案1
您正在尝试打开一个文件。您可以运行如下命令:
open (PS, "-|", "fping 192.168.$IPrange.$IPendnum");