为什么在以下 Perl 脚本中使用未初始化的值时会收到警告?

发布于 2024-09-08 08:19:57 字数 711 浏览 2 评论 0原文

我正在尝试减少此列表中打印的端口数量:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2024-09-15 08:19:57

最有可能的是,表达式 $hostz->tcp_ports('open') 返回 undef,而不是您期望的数字。

Most likely, the expression $hostz->tcp_ports('open') returned undef, not a number like you expected.

小伙你站住 2024-09-15 08:19:57

以下是如何从包含端口列表的字符串中选择感兴趣的端口:

#!/usr/bin/perl

my %interesting = map { $_ => undef } qw( 80 445 515 9100 );
(undef, my $str) = split ' ', q{A.B.C.D 80,280,443,515,631,7627,9100,14000};

my @ports = grep { exists $interesting{$_} } split /,/, $str;

print "Interesting ports: [@ports]\n";

以下是我将如何重写您的代码:

#!/usr/bin/perl

my %interesting = map { $_ => undef } qw( 80 445 515 9100 );

for my $ip (@ips_sorted) {
    print "$ip\t";
    my $hostz = $np->get_host($ip);
    unless ( defined $hostz ) {
        warn "get_host returned undef for ip: '$ip'\n";
        next;
    }
    my $port = $hostz->tcp_ports('open');

    unless ( defined $port ) {
        warn "tcp_ports returned undef for ip: '$ip'\n";
        next;
    }

    (undef, my $str) = split ' ', $port;
    my @ports = grep { exists $interesting{$_} } split /,/, $str;

    if ( @ports ) {
        print join(',', @ports);
    }
    print "\n"
}

Here is how one can select the interesting ports from a string containing the list of ports:

#!/usr/bin/perl

my %interesting = map { $_ => undef } qw( 80 445 515 9100 );
(undef, my $str) = split ' ', q{A.B.C.D 80,280,443,515,631,7627,9100,14000};

my @ports = grep { exists $interesting{$_} } split /,/, $str;

print "Interesting ports: [@ports]\n";

Here is how I would re-write your code:

#!/usr/bin/perl

my %interesting = map { $_ => undef } qw( 80 445 515 9100 );

for my $ip (@ips_sorted) {
    print "$ip\t";
    my $hostz = $np->get_host($ip);
    unless ( defined $hostz ) {
        warn "get_host returned undef for ip: '$ip'\n";
        next;
    }
    my $port = $hostz->tcp_ports('open');

    unless ( defined $port ) {
        warn "tcp_ports returned undef for ip: '$ip'\n";
        next;
    }

    (undef, my $str) = split ' ', $port;
    my @ports = grep { exists $interesting{$_} } split /,/, $str;

    if ( @ports ) {
        print join(',', @ports);
    }
    print "\n"
}
最后的乘客 2024-09-15 08:19:57

$hostz->tcp_ports('open') 可能会返回一个端口列表,您应该将其存储在数组变量中:@ports 而不是 $ports< /代码>。然后你应该检查数组的每个元素。

您也可以只用一行来完成:

$output = join(',', grep { $_ != 80 && $_ != 445 && $_ != 515 && $_ != 9100 } $hostz->tcp_ports('open'));

$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:

$output = join(',', grep { $_ != 80 && $_ != 445 && $_ != 515 && $_ != 9100 } $hostz->tcp_ports('open'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文