Skip to content

Instantly share code, notes, and snippets.

@jpmens
Last active November 28, 2021 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmens/32d7c5804b83a6406fbbdcb442cfadc1 to your computer and use it in GitHub Desktop.
Save jpmens/32d7c5804b83a6406fbbdcb442cfadc1 to your computer and use it in GitHub Desktop.
Create a MaxMind database ISP.mmdb for BIND's named
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw( say );
use local::lib 'local';
use MaxMind::DB::Writer::Tree;
use Net::Works::Network;
my $filename = 'GeoIP2-ISP.mmdb'; # JPM
# Your top level data structure will always be a map (hash). The MMDB format
# is strongly typed. Describe your data types here.
# See https://metacpan.org/pod/MaxMind::DB::Writer::Tree#DATA-TYPES
my %types = (
isp => 'utf8_string', # JPM
);
my $tree = MaxMind::DB::Writer::Tree->new(
# "database_type" is some arbitrary string describing the database. At
# MaxMind we use strings like 'GeoIP2-City', 'GeoIP2-Country', etc.
database_type => 'My-IP-Data',
# "description" is a hashref where the keys are language names and the
# values are descriptions of the database in that language.
description =>
{ en => 'My database of IP data', fr => q{Mes donnees IP}, },
# "ip_version" can be either 4 or 6
ip_version => 4,
# add a callback to validate data going in to the database
map_key_type_callback => sub { $types{ $_[0] } },
# "record_size" is the record size in bits. Either 24, 28 or 32.
record_size => 24,
# permit RFC 1918 networks (JPM)
remove_reserved_networks => 0,
);
my %address_for_employee = ( # JPM
'192.168.1.140/32' => {
isp => 'jpmens',
},
'192.168.33.0/24' => {
isp => 'Jane',
},
'127.0.0.0/8' => {
isp => 'rabbit',
},
);
for my $address ( keys %address_for_employee ) {
# Create one network and insert it into our database
my $network = Net::Works::Network->new_from_string( string => $address );
$tree->insert_network( $network, $address_for_employee{$address} );
}
# Write the database to disk.
open my $fh, '>:raw', $filename;
$tree->write_tree( $fh );
close $fh;
say "$filename has now been created";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment