Net::Subnets(3pm)

πŸ“¦
version 1.02 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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 (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