如何对IP地址列表进行排序并为每个地址计算类和网络掩码? [英] How do I sort a list of IP addresses and compute a class and netmask for each?

查看:205
本文介绍了如何对IP地址列表进行排序并为每个地址计算类和网络掩码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择。一个是我有一个包含IP列表的数组。例如

I have 2 options. One is that I have a array that has a list of IPs. For example

 my @Ip=();  # array that has the IPs below in it

示例输入:

108.0.0.30
108.0.0.30
108.0.0.30
192.168.1.1
192.168.1.2
10.0.0.1

我需要一个可以对这样的数组进行排序并告诉它是哪个网络类和子网掩码的程序。例如,输出应该类似于

I need a program that can sort such an array and tell which network class and subnet mask it is. For example, the output should be like

10.1.1.1/25                          10.1.1.1 is ip and 25 is submask


推荐答案

Net :: IP Net :: IP :: Resolver Net :: IP :: Match :: Regexp 和其他子模块来自 Net :: IP 正在为你做这件事。只是排序的部分很难。但如果你谷歌它,你会发现一些不错的方法。例如:

Net::IP, Net::IP::Resolver, Net::IP::Match::Regexp and other Submodules from Net::IP are doing that fine for you. Just the part with sorting is difficult. But if you google it, you find some nice Methods. For example:

my @ips = qw(
    172.27.32.200
    172.19.32.100
    10.1.1.60
    192.20.30.133
);

@ips = map {s/\s+//g; $_} sort map {s/(\d+)/sprintf "%3s", $1/eg; $_} @ips;

print join "\n", @ips;

在这里找到

这篇关于如何对IP地址列表进行排序并为每个地址计算类和网络掩码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆