while循环对齐列

| 你好 我编写了一个perl脚本,在其中将填充了ip和端口扫描的文本文件中的列存储到变量中。变量包含许多ip地址,端口,协议,状态和服务,现在我需要一个while循环,该循环将所有存储在ip变量中的ip \与其对应的端口协议和状态等进行匹配。所以: 192.168.3 45 tcp打开smtp 这是我的代码
$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;
}
    
已邀请:
通常,我说要使用您了解的工具,并且可以执行诸如从Perl调用
awk
之类的事情。但是对于此代码,我将做一个例外。您应该使用内置的
Perl
命令执行此任务。即数组和Perl的
grep
运算符。这是我将开始重写此方法的方式。
# 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;
    
我通常会尝试一次完成这些事情,例如...
#!/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;

}
    

要回复问题请先登录注册