在 *Nix 环境中提取至少 3 个会话的 IP 地址

发布于 2024-10-14 17:02:26 字数 1234 浏览 2 评论 0原文

如图所示,我们有一个 IP 地址列表(请注意,128.3.* 和 131.243.*)位于我们的子网内。我们更喜欢用 Perl 来做,任何其他好的想法在 *nix 环境中都更受欢迎。

图片格式如下:

目标 IP(已排序)- Dest_Port - 源IP- Source_Port

空间用作分隔符。

我们是手动完成的,但知道我们有巨大的文件并且希望自动化此过程。

问题是我们需要得到 目标 IP 地址(不在我们的子网中,不是 128.3.* 和 131.243.*) 与至少 3 个不同的人进行过沟通 IP 地址(来源)在我们的子网内 (128.3.* 和 131.243.*)。

在此处输入图像描述

例如 117.72.15.207 与至少 3 个不同的

59.69.194.12 80 131.243.93.74 4492
59.79.35.247 80 131.243.94.123 1307
59.100.23.87 80 131.243.92.72 45577
*117.72.15.207 80 *131.243.92.10 451 
*117.72.15.207 80 *131.243.92.117 21071
117.72.15.207 80 131.243.92.117 21072
117.72.15.207 80 131.243.92.117 21073
*117.72.15.207 80 *131.243.92.191 9248

输出将为:

117.72.15.207 80 131.243.92.10 451 
117.72.15.207 80 131.243.92.117 21071
117.72.15.207 80 131.243.92.191 9248

如果不清楚,请发表评论。谢谢...

As seen in the picture, we have a list of IP addresses (Please note that 128.3.* and 131.243.*) are inside our subnet. We prefer to do it in Perl, any other good ideas more than welcome in *nix environment.

The pictures is formatted as:

Destination IP(sorted)- Dest_Port
- Source IP- Source_Port

space is used as a separator.

We were doing it manually but know we have huge files and want to automate this procedure.

The problem is we need to get the
destination IP addresses (not in our subnet, not 128.3.* and 131.243.*) which are
communicated with at least 3 different
IP addresses (source) inside our subnet
(128.3. * and 131.243.*) .

enter image description here

As an example 117.72.15.207 communicates (not subnet ip) with at least 3 different source addresses are in our subnet (131.243.92.10, 131.243.92.117 and 131.243.92.191). Please note that it communicates with 131.243.92.117 more than one with different source port addresses so it will count only one source address in output. i put a star to specify the condition.
we need the list of IP pairs who mets this condition

59.69.194.12 80 131.243.93.74 4492
59.79.35.247 80 131.243.94.123 1307
59.100.23.87 80 131.243.92.72 45577
*117.72.15.207 80 *131.243.92.10 451 
*117.72.15.207 80 *131.243.92.117 21071
117.72.15.207 80 131.243.92.117 21072
117.72.15.207 80 131.243.92.117 21073
*117.72.15.207 80 *131.243.92.191 9248

The output will be:

117.72.15.207 80 131.243.92.10 451 
117.72.15.207 80 131.243.92.117 21071
117.72.15.207 80 131.243.92.191 9248

Please comment if something is unclear. Thanks...

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

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

发布评论

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

评论(1

小巷里的女流氓 2024-10-21 17:02:26
#!/usr/bin/env perl

use strict;

my %ip_lookup;
while (<DATA>) {
    my ($dest_ip, $dest_port, $source_ip, $source_port) = (split ('\s+', $_));
    $ip_lookup{$dest_ip}{$source_ip} = $_ unless $ip_lookup{$dest_ip}{$source_ip};
}

for my $dest_ip (keys %ip_lookup) {
    if (scalar(keys %{$ip_lookup{$dest_ip}}) >= 3) {
        print $_ for sort values %{$ip_lookup{$dest_ip}};
    }
}

__DATA__
9.69.194.12 80 131.243.93.74 4492
59.79.35.247 80 131.243.94.123 1307
59.100.23.87 80 131.243.92.72 45577
117.72.15.207 80 131.243.92.10 451 
117.72.15.207 80 131.243.92.117 21071
117.72.15.207 80 131.243.92.117 21072
117.72.15.207 80 131.243.92.117 21073
117.72.15.207 80 131.243.92.191 9248

在 Perl 中有几种方法可以做到这一点,但我上面发布的示例很容易解释。

首先,它读取文件的每一行(我使用的是 DATA 句柄,但它与文件的工作方式相同),然后拆分该行以获得不同的 ip、端口组合。

然后它会填充一个多级散列,除非之前已经见过这个目标 - 源 IP 组合(您创建的不同端口点)。

最后,它会排序并循环检查是否为每个目标 IP 创建了 3 个或源 IP 条目,如果是的话,则打印出该行。

这会产生输出:

117.72.15.207 80 131.243.92.10 451 
117.72.15.207 80 131.243.92.117 21071
117.72.15.207 80 131.243.92.191 9248

这就是您所需要的。

#!/usr/bin/env perl

use strict;

my %ip_lookup;
while (<DATA>) {
    my ($dest_ip, $dest_port, $source_ip, $source_port) = (split ('\s+', $_));
    $ip_lookup{$dest_ip}{$source_ip} = $_ unless $ip_lookup{$dest_ip}{$source_ip};
}

for my $dest_ip (keys %ip_lookup) {
    if (scalar(keys %{$ip_lookup{$dest_ip}}) >= 3) {
        print $_ for sort values %{$ip_lookup{$dest_ip}};
    }
}

__DATA__
9.69.194.12 80 131.243.93.74 4492
59.79.35.247 80 131.243.94.123 1307
59.100.23.87 80 131.243.92.72 45577
117.72.15.207 80 131.243.92.10 451 
117.72.15.207 80 131.243.92.117 21071
117.72.15.207 80 131.243.92.117 21072
117.72.15.207 80 131.243.92.117 21073
117.72.15.207 80 131.243.92.191 9248

There are a few ways to do this in perl but the sample I've posted above is simple to explain.

First it's reading each line of the file in (I'm using the DATA handle but it'll work same with file), then splitting the line to get the different ip, port combinations.

Then it populates a multi-level hash unless this destination - source ip combination has been seen before (The differing ports point you made).

Finally it sorts and loops through checking if 3 or source IP entries have been made for each destination IP printing out the line if this is true.

This results in the output:

117.72.15.207 80 131.243.92.10 451 
117.72.15.207 80 131.243.92.117 21071
117.72.15.207 80 131.243.92.191 9248

which is what you required.

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