首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >while循环对齐列

while循环对齐列
EN

Stack Overflow用户
提问于 2011-04-16 00:59:38
回答 2查看 303关注 0票数 0

您好,我编写了一个perl脚本,将文本文件中充满ip和端口扫描的列存储到变量中。这些变量包含许多ip地址、端口、协议、状态和服务。现在我需要一个while循环,它获取存储在ip变量中的所有ip,并将它们与其相应的端口协议和状态etc.side进行匹配。

下面是我的代码

代码语言:javascript
复制
$ip_address = `cat /cygdrive/c/Windows/System32/test11.txt | 
    grep 'Nmap scan report for'`;

$state = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'PORT'|
    grep -v 'filtered'| grep -v 'latency'| grep -v 'Nmap' | grep -v 'Discovered' |
    grep -v 'Raw' | grep -v 'SYN' | grep -v 'DNS'| grep -v 'Ping' | 
    grep -v 'Scanning' `;

$port = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v  'Discovered'| 
    grep -v 'Nmap' | grep -v 'PORT' | grep -v 'ports'| grep -v 'Read' | 
    grep -v 'Raw'| grep -v 'Completed'| grep -v 'DNS' | grep -v 'hosts' | 
    grep -v 'Ping' | grep -v 'SYN' | grep -v 'latency' `;

$protocol = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v  'Discovered'| 
    grep -v 'Nmap' | grep -v 'PORT' | grep -v 'ports'| grep -v 'Read' | 
    grep -v 'Raw' | grep -v 'Completed'| grep -v 'DNS' | grep -v 'hosts' | 
    grep -v 'Ping' | grep -v 'SYN' | grep -v 'latency' `;
{

$service = `cat /cygdrive/c/Windows/System32/test11.txt | grep -v 'Nmap' | 
    grep-v 'Host' | grep -v 'filtered' | grep -v 'PORT' | grep -v 'Raw'| 
    grep -v 'Scanning'| grep -v 'Completed'| grep -v 'Ping' |grep -v 'DNS' | 
    grep -v 'Discovered'| grep -v 'SYN'`;

while($ip_address, $port, $protocol, $state, #service)
{
    chomp ($ip_address, $port, $protocol, $state, #service);
    print "$ip_address, $port, $protocol, $state, #service";
    exit 0;
}
EN

回答 2

Stack Overflow用户

发布于 2011-04-16 01:18:19

通常,我建议使用您理解的工具,并且可以做一些事情,比如从Perl调用awk。但是对于这段代码,我会做一个例外。您应该使用内置的Perl命令来执行此任务。即数组和Perl的grep运算符。下面是我将如何开始重写这段代码。

代码语言:javascript
复制
# do this once instead of `cat ...` several times.
open my $fh, '<', '/cygdrive/c/Windows/System32/test11.txt';
my @the_input = <$fh>;
close $fh;

# do this instead of `| grep -v ... | grep -v ...`
my @ip_addresses = grep { /Nmap scan report for/ } @the_input;
my @states = grep { 
    !/PORT|filtered|latency|Nmap|Discovered|Raw|SYN|DNS|Ping|Scanning/
} @the_input;
my @ports = grep {
    !/Discovered|Nmap|PORT|ports|Read|Raw|Completed|DNS|hosts|Ping|SYN|latency/
} @the_input;
my @protocols = grep {
    !/Discovered|Nmap|PORT|ports|Read|Raw|Completed|DNS|hosts|Ping|SYN|latency/
} @the_input;
my @services = grep {
    !/Nmap|Host|filtered|PORT|Raw|Scanning|Completed|Ping|DNS|Discovered|SYN/
} @the_input;
票数 1
EN

Stack Overflow用户

发布于 2011-04-16 10:35:49

我通常试着一次完成这类事情,就像...

代码语言:javascript
复制
#!/usr/bin/perl

open(F, "/cygdrive/c/Windows/System32/test11.txt");

while(<F>) {

  # If the current line has something that matches an IP
  # address, store the matched pattern in $ip. We'll
  # use this as we process the remaining lines.
  #
  $ip = $1 if ( /Nmap scan report for (\d+\.\d+\.\d+\.\d+)/ );

  # Try to match lines like "ddd/www www www wwww"
  #
  ( $port, $protocol, $state, $service) = ( m|(\d+)/(\w+)\s+(\w+)\s+(\w+)| );

  print "$ip, $port, $protocol, $state, $service\n" if $port;

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5680014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档