为什么在以下 Perl 脚本中使用未初始化的值时会收到警告?
我正在尝试减少此列表中打印的端口数量:
ABCD 80,280,443,515,631,7627,9100,14000
到我最感兴趣的:
<代码>ABCD 80,515,9100
为此,我使用了这段代码:
foreach (@ips_sorted) {
print "$_\t";
my $hostz = $np->get_host($_);
my $port = 0;
my $output = 0;
$port = $hostz->tcp_ports('open');
if ($port == 80 || $port == 445 || $port == 515 || $port == 9100) {
$output = join ',' ,$port;
}
print $output;
print "\n";
}
我可能不需要说它不起作用。我明白了:
<代码>ABCD 0
在 parse-nmap-xml.pl 行 **(带有 if 的行)的数字 eq (==) 中使用未初始化值 $port。
I am trying to cut down the number of ports printed in this list:
A.B.C.D 80,280,443,515,631,7627,9100,14000
to the ones that are most interesting for me:
A.B.C.D 80,515,9100
To do this, I am using this bit of code:
foreach (@ips_sorted) {
print "$_\t";
my $hostz = $np->get_host($_);
my $port = 0;
my $output = 0;
$port = $hostz->tcp_ports('open');
if ($port == 80 || $port == 445 || $port == 515 || $port == 9100) {
$output = join ',' ,$port;
}
print $output;
print "\n";
}
I probably don't need to say, that it's not working. I get this:
A.B.C.D 0
Use of uninitialized value $port in numeric eq (==) at parse-nmap-xml.pl line **(line with if).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最有可能的是,表达式
$hostz->tcp_ports('open')
返回undef
,而不是您期望的数字。Most likely, the expression
$hostz->tcp_ports('open')
returnedundef
, not a number like you expected.以下是如何从包含端口列表的字符串中选择感兴趣的端口:
以下是我将如何重写您的代码:
Here is how one can select the interesting ports from a string containing the list of ports:
Here is how I would re-write your code:
$hostz->tcp_ports('open')
可能会返回一个端口列表,您应该将其存储在数组变量中:@ports
而不是$ports< /代码>。然后你应该检查数组的每个元素。
您也可以只用一行来完成:
$hostz->tcp_ports('open')
probably returns a list of ports which you should store in an array variable:@ports
instead of$ports
. Then you should check each element of the array.You can do it also in just one line: