How do I sort a list of IP addresses and compute a class and netmask for each? [closed]

有些话、适合烂在心里 提交于 2019-12-31 07:59:34

问题


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

Sample input:

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

回答1:


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;

Found here



来源:https://stackoverflow.com/questions/17506758/how-do-i-sort-a-list-of-ip-addresses-and-compute-a-class-and-netmask-for-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!