Using just a single Brik for a quick program

Today, we will show you how to use just a single Brik to build a standalone program without installing a multitude of packages or modules. To achieve that, we will use the brik::tool which is a helper to install dependencies, et use the classic lookup::iplocation Brik as the single Brik to use.

Note: you will need version 1.22 for this to work.

Install or update The Metabrik Platform

Installing should be as easy as running:

sudo cpan install Metabrik
sudo cpan install Metabrik::Repository

If that does not work for you, you can follow more complete installation instructions.

Then, you can update the platform at any time to latest repository version with this one-liner:

perl -MMetabrik::Core::Context -e 'Metabrik::Core::Context->new_brik_run("brik::tool", "update")'

You could put an alias in your shell to help doing so in the future:

alias update-metabrik="perl -MMetabrik::Core::Context -e 'Metabrik::Core::Context->new_brik_run(
\"brik::tool\", \"update\")'"

Installing a Brik dependencies

As a Brik may have some package(s) or module(s) dependencies, we’ve put some effort into simplifying their installation. There is a brik::tool install Command dedicated to that. It will even know when to use sudo for you. Use this one-liner to install dependencies for our example lookup::iplocation Brik:

perl -MMetabrik::Core::Context -e 'Metabrik::Core::Context->new_brik_run("brik::tool", "install", "lookup::iplocation")'

You could also have used The Metabrik Shell by launching metabrik.sh:

messiah:~> run brik::tool install lookup::iplocation

Creating a meta-tool

Now everything is in place, you need to know which Commands are available for the lookup::iplocation Brik. The easiest way is to use The Metabrik Shell help Command. And don’t forget to use <tab> keystroke to use completion at each step:

messiah:~> use lookup::iplocation
[*] core::shell: use: Brik [lookup::iplocation] success
messiah:~> help lookup::iplocation
[+] set lookup::iplocation datadir <datadir>
[+] run lookup::iplocation from_ip <ip_address>
[+] run lookup::iplocation from_ipv4 <ipv4_address>
[+] run lookup::iplocation from_ipv6 <ipv6_address>
[+] run lookup::iplocation organization_name <ip_address>
[+] run lookup::iplocation subnet4 <ipv4_address>
[+] run lookup::iplocation update

Two Commands are of interest here: update and from_ip. The first one let’s you get latest versions of the Maxmind IP geolocation database.

You now know what you want to do, let’s use another brik::tool Command to create a meta-tool skeleton:

perl -MMetabrik::Core::Context -e 'Metabrik::Core::Context->new_brik_run("brik::tool", "create_tool", "lookup-iplocation.pl")'

Or from The Metabrik Shell by launching metabrik.sh:

messiah:~> run brik::tool create_tool lookup-iplocation.pl

And you populate the generated content skeleton with required code to call update and from_ip Commands:

#!/usr/bin/env perl
#
# $Id$
#
use strict;
use warnings;

my $ip = shift or die("$0 ");

# Uncomment to use a custom repository
#use lib qw(/lib);

use Data::Dumper;
use Metabrik::Core::Context;
use Metabrik::Lookup::Iplocation;

my $con = Metabrik::Core::Context->new or die("core::context");

# Init other Briks here
my $li = Metabrik::Lookup::Iplocation->new_from_brik_init($con) or die("lookup::iplocation");
$li->update or die("update failed");

# Put Metatool code here
print Dumper($li->from_ip($ip))."\n";

exit(0);

And voilà. Test your program:

perl lookup-iplocation.pl 93.184.216.34
[+] mirror: file [/home/gomor/metabrik/lookup-iplocation/GeoIPv6.dat.gz] not modified since last check
[+] mirror: file [/home/gomor/metabrik/lookup-iplocation/GeoIP.dat.gz] not modified since last check
[+] mirror: file [/home/gomor/metabrik/lookup-iplocation/GeoIPCity.dat.gz] not modified since last check
[+] mirror: file [/home/gomor/metabrik/lookup-iplocation/GeoIPASNum.dat.gz] not modified since last check
$VAR1 = {
          'country_code3' => 'USA',
          'metro_code' => 506,
          'city' => 'Norwell',
          'dma_code' => 506,
          'country_code' => 'US',
          'postal_code' => '02061',
          'country_name' => 'United States',
          'continent_code' => 'NA',
          'region_name' => 'Massachusetts',
          'longitude' => '-70.8228',
          'region' => 'MA',
          'area_code' => 781,
          'latitude' => '42.1508'
        };

Alternatively, for a so simple task, you could have used The Metabrik Shell:

messiah:~> use lookup::iplocation
[*] core::shell: use: Brik [lookup::iplocation] success
messiah:~> run lookup::iplocation update
messiah:~> run lookup::iplocation from_ip 93.184.216.34
{
  area_code      => 781,
  city           => "Norwell",
  continent_code => "NA",
  country_code   => "US",
  country_code3  => "USA",
  country_name   => "United States",
  dma_code       => 506,
  latitude       => 42.1508,
  longitude      => -70.8228,
  metro_code     => 506,
  postal_code    => "02061",
  region         => "MA",
  region_name    => "Massachusetts",
}

Conclusion

We have shown how to get up-to-date with The Metabrik Platform and how to develop a meta-tool. Helped with that, you can start to develop programs using the 200+ available Briks. For instance, try to add lookup::threatlist support to lookup-iplocation.pl as an exercise.