while 循环对齐列

发布于 2024-11-01 23:33:47 字数 1608 浏览 1 评论 0原文

你好 我编写了一个 perl 脚本,其中将填充 ip 和端口扫描的文本文件中的列存储到变量中。这些变量包含许多 ip 地址、端口、协议、状态和服务,现在我需要一个 while 循环,它获取 ip 变量中存储的所有 ip,并将它们与其相应的端口、协议和状态等进行匹配。并排看起来如下: 192.168.3 45 tcp open 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;
}

hi
I wrote a perl script where I stored columns from a text file filled with ip and port scans into variables. The variables contain many ip addreses, ports, protocols, states, and services now I need a while loop that takes all the ip's stored in the ip variable and matches them with its corresponding ports protocols and state etc.side by side look so:
192.168.3 45 tcp open smtp

heres my code

$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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故笙诉离歌 2024-11-08 23:33:47

通常我会说使用您理解的工具,并且可以执行诸如从 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;

Usually I say to use the tools that you understand and that it's OK to do things like call awk from Perl. But for this code I'll make an exception. You should be using the builtin Perl commands for this task. Namely, arrays and Perl's grep operator. Here's how I would start to rewrite this.

# 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;
任谁 2024-11-08 23:33:47

我通常尝试一次性完成这些事情,比如......

#!/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;

}

I usually try to do these sorts of things in one pass, like...

#!/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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文