diddi- / libphpipam-perl

Perl module to work with the phpIPAM database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

phpipam

phpipam - Module to work with the phpIPAM (phpipam.net) database

SYNOPSIS

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use phpipam;

my $ipam = phpIPAM->new(
    dbhost => 'localhost',
    dbuser => 'phpipam',
    dbpass => 'phpipam',
    dbname => 'phpipam',
    dbport => 3306,
);

if(not $ipam) {
    print "ERROR could not create object\n";
    return -1;
}

my $ret = $ipam->getAllSubnets();

print Dumper($ret);

my $ipv4 = $ipam->getIP("173.194.70.100");
print Dumper($ipv4);
my $ipv6 = $ipam->getIP("2a00:1450:4001:c02::66");
print Dumper($ipv6);
exit(0);

INSTALLATION

These are the steps to install the module

perl Makefile.PL
make
make install

DEPENDENCIES

phpipam have some dependencies to other modules

Required

Carp
DBI
DBD::mysql
Net::IP
Exporter

DESCRIPTION

phpipam is a helper module to retrieve information from the phpipam database (phpipam.net)

EXPORT

None by default.

METHODS

new()

Calling new() with valid options will automatically try and connect to the phpIPAM database. If successful, a blessed object is returned to the user.

dbhost => [hostname|ip]     - DNS hostname or IP address (IPv4 or IPv6)
                              of the remote phpIPAM MySQL database.
                              ( Default: localhost )

dbport => port              - Port number to connect to (1-65535).
                              ( Default: 3306 )

dbuser => string            - Username to use when authenticating to the
                              MySQL database.
                              ( Default: phpipam )

dbpass => string            - Password to use when authenticating to the
                              MySQL database.
                              ( Default: phpipam )

dbname => string            - Name of the MySQL database where phpIPAM
                              stores all it's data.
                              ( Default: phpipam )

getAllSubnets()

Returns an array of hashes with each subnet stored in the database. This function does not care about sections and relations between subnets.

$phpipam->getAllSubnets();

If successful, returns a data structure similar to the one below: $VAR1 = [ { 'vrfId' => '0', 'description' => 'Sample Subnet', 'mask' => '16', 'id' => '29', 'subnet' => '176160768', 'sectionID' => '4' }, ]; Returns -1 if the query is unsuccessful for any reason.

getSubnets(%opts)

Returns an array of hashes with each subnet within a section and/or vrf.

$phpipam->getSubnets({vrf => "testvrf", section => "Servers");

Note that phpIPAM does not yet have any uniqueness checking on VRFs. This means that multiple VRFs with the same name and the same RD may exist at the same time within the phpIPAM database. If 'vrf' is given as an option, getSubnets() will return the subnets from the first VRF it matches in the database, all other VRFs are silently ignored.

If no options are given, getSubnets() behave just as getAllSubnets() do.

getAllSections()

Returns an array of hashes with each section stored in the database.

$phpipam->getAllSections();

getAllVrfs()

Returns an array of hashes with each VRF stored in the database.

$phpipam->getAllVrfs();

getSiteTitle

Returns the site Title (what you see in the phpIPAM banner after logging in).

$phpipam->getSiteTitle();

getIP($ip)

Returns a hash with information about a specific IP address.

$phpipam->getIP("173.194.70.100");

If successful, returns a data structure similar to the one below:

$VAR1 = [
      {
        'description' => 'An IP Address description',
        'id' => '92',
        'port' => '',
        'mac' => '',
        'owner' => 'Admin',
        'subnetId' => '29',
        'switch' => '',
        'state' => '1',
        'dns_name' => 'dns.as.seen.in.the.database.com'
      }
    ];

getSubnet(%opts)

Return an array of hashes with information about the subnet(s) within the given section and/or vrf. Options are

section => string           - Section name as stored in the database.
                              Section names are case sensitive.

subnet => CIDR              - IPv4 or IPv6 CIDR as stored in the database.
                              NOTE: phpipam does not do any calculations
                              on subnets, a subnet must exactly match what's in
                              the database.
                              This argument is MANDATORY.

vrf => [name|RD]            - Name or Route-Distinguisher of the VRF to search in.

$phpipam->getSubnet("192.168.0.0/24");

If more than one subnet is found (which is likely if no section or vrf is given), getSubnet() will return an array with all subnets found.

getVrf([$vrf|$rd])

Returns a single-element array with a hash containing all information about the given vrf.

$phpipam->getVrf("testvrf");
$phpipam->getVrf("123:123");

If more than one VRF is found with the given name, only the first match is returned. getVrf() matches on name first, then RD. This means that if one VRF is stored in the database, and another VRF is stored with an RD being the same as the first VRF name - trying to find the VRF using RD can be quite difficult.

Consider this VRF1 Name: 123:123 RD: weird-RD

VRF2
    Name: AnotherVRF
    RD:   123:123

$phpipam->getVrf("123:123");

In the above example, getVrf() will return only information about VRF1. If you want to be able to distinguish between the VRFs, make sure that names and RDs do not collide in the database.

getSection($section)

Returns a single-element array with a hash containing all information about the given section.

$phpipam->getSection("TestSection");

getAddresses(%opts)

Returns an array of hashes with information about a all addresses within a specific Section, VRF or subnet based on the given options

section => string           - Section name as stored in the database.
                              Section names are case sensitive.

subnet => CIDR              - IPv4 or IPv6 CIDR as stored in the database.
                              NOTE: phpipam does not do any calculations
                              on subnets, a subnet must exactly match what's in
                              the database.

vrf => [name|RD]            - Name or Route-Distinguisher of the VRF to search in.

filter => {key => value}    - Where key is the database column name. Filters addresses
                              that only matches the filter.

All options are optional, getAddresses will try its best to match addresses only found within the section, subnet or vrf.

$phpipam->getAddresses({section => "server", subnet => "10.0.0.0/8"});
$phpipam->getAddresses({subnet => "172.16.0.0/24", vrf => "TestVRF"});

By default, getAddresses return all addresses of all sections and subnets if no options are given.

SEE ALSO

phpIPAM official homepage - http://phpipam.net

AUTHOR

Diddi Oscarsson, <diddi@diddi.se>

COPYRIGHT AND LICENSE

Copyright (C) 2013 by Diddi Oscarsson

The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Perl module to work with the phpIPAM database

License:MIT License


Languages

Language:Perl 100.0%