Net::Subnets(3pm)
π Net::Subnets β Computing Subnets In Large Scale Networks π
okay bestie, so this is a page from the User Contributed Perl Documentation manual set, and it's literally about matching IP addresses against CIDR subnets at scale. no cap. let's get into it π¦
β¨ SYNOPSIS β¨
like, the whole vibe in one little block:
use Net::Subnets;
my $sn = Net::Subnets->new;
$sn->subnets(\@subnets);
if (my $subnetref = $sn->check(\$address)) {
...
}
my ($lowipref, highipref) = $sn->range(\$subnet);
my $listref = $sn->list(\($lowipref, $highipref));
yes that's the whole API and yes it's giving main character energy π
πΈ DESCRIPTION πΈ
okay so the actual description: Very fast matches large lists of IP addresses against many CIDR subnets and calculates IP address ranges. that's the tea π
here's a simple and efficient example for subnet matching, and honestly it ate:
use Net::Subnets;
my @subnets = qw(10.0.0.0/24 10.0.1.0/24);
my @addresses = qw/10.0.0.1 10.0.1.2 10.0.3.1/;
my $sn = Net::Subnets->new;
$sn->subnets(\@subnets);
my $results;
foreach my $address (@addresses) {
if (my $subnetref = $sn->check(\$address)) {
$results .= "$address: $$subnetref\n";
}
else {
$results .= "$address: not found\n";
}
}
print($results);
and this one is a simple example for range calculation (look at it go π₯):
use Net::Subnets;
my @subnets = qw(10.0.0.0/24 10.0.1.0/24);
my $sn = Net::Subnets->new;
my $results;
foreach my $subnet (@subnets) {
my ($lowipref, $highipref) = $sn->range(\$subnet);
$results .= "$subnet: $$lowipref - $$highipref\n";
}
print( $results );
and finally, a simple example for list generation, because sometimes you just need every single address in the range, bestie:
use Net::Subnets;
my $lowip = '192.168.0.1';
my $highip = '192.168.0.100';
my $sn = Net::Subnets->new;
my $listref = $sn->list(\($lowip, $highip));
foreach my $address (@$listref) {
# do something cool
}
π« METHODS π«
π new
my $subnets = Net::Subnets->new;
Creates an Net::Subnets object. that's it, that's the whole method. iconic of it honestly π
π subnets
$subnets->subnets([qw(10.0.0.0/24 10.0.1.0/24)]);
The subnets method lets you prepare a list of CIDR subnets. like, get your subnets ready before you start checking things against them π«Ά
π check
my $match = $subnets->check(\$address);
The check method lets you check an IP address against the previously prepared subnets. note the previously prepared part β do your subnets call first or it's giving nothing π
π range
my ($lowest, $highest) = $subnets->range(\$subnet)
The range method lets you calculate the IP address range of a subnet. two refs back, lowest and highest, boom β¨
π list
my $list = $subnets->list(\$lowest, $highest);
The list method lets you calculate a list containing all IP addresses in a given range. every single one. hydrated and unbothered π
π AUTHOR π
Sebastian Riedel (sri@cpan.org), Juergen Peters (juergen.peters@taulmarill.de)
legends, fr fr π
π COPYRIGHT AND LICENSE π
Copyright (C) 2003-2010, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. slay.
and that's the whole page bestie, you're a certified subnet girlie now π§
perl v5.36.0 Β· 2022-10-15 Β· Net::Subnets(3pm) β mwah, xoxo, go match some subnets ππΈπ
π¦ where this page comes from
- ubuntu noble: libnet-subnets-perl 1.02-3 Β· Artistic-2.0
- ubuntu resolute: libnet-subnets-perl 1.02-3 Β· Artistic-2.0
- ubuntu stonking: libnet-subnets-perl 1.02-3 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms